Python – Modules
Python – Modules: As the software grows in length, it might be separated into many files to make maintenance easier. A file containing all relevant functions may be produced and included in several programmes of the application without having to duplicate it in each one. The module is the name given to such a file.
Create a Module
A Python – Modules can be named anything and have the .py file extension.
Example:
The code for a module named MyModule is shown in the example below. Please remember that the module’s file extension must be.py. Two functions, MyFunc and MyFunction, are generated in this module. MyFunc displays Hello World! with no parameters, but MyFunction accepts two numbers as inputs and returns the sum of those numbers. In addition, it has a variable named MyVar.
def MyFunc(): print("Hello World!.") def MyFunction(a, b): return a+b MyVar = 1000
Use a Module
To utilise a module, use the Python keyword import followed by the module name in the current script. The module will be imported with the mentioned statement if it is maintained in the same folder as the current script; otherwise, its path must be shown in the current script. All functions, variables, classes, and other items specified in the module can be utilised in the current script by prefixing their names with module-name followed by a dot (.).
Example:
The above-mentioned module MyModule is imported in the current script in the example below. Because the module is stored in a different folder, one of the most often used methods is used to set the module’s path (using sys.path.append function after importing sys module). The syntax is shown below. In the present script, the MyFunc() function from MyModule is utilised using the syntax MyModule. MyFunc().
import sys sys.path.append('path to module') import MyModule MyModule.MyFunc()
The output of the above code will be:
Hello World!.
Re-naming a Module
The as keyword can be used to rename the imported module. For syntax, see the sample below. All functions, variables, classes, and other items created in the module may now be utilised in the current script by prefixing their names with new name followed by a dot (.).
Example:
The module MyModule is imported into the current script with the name MMod in the example below. The MyFunc() function from MyModule is then called using the syntax MMod in the current script. MyFunc().
import sys sys.path.append('path to module') import MyModule as MMod MMod.MyFunc()
The output of the above code will be:
Hello World!.
Import from Module
Instead of importing the whole module, individual elements of the module, such as a single function, variable, or class, can be imported using the from and import keywords in the current script. For syntax, see the sample below. The imported pieces can be utilised in the current script using their names without a prefix using this technique.
Example:
In the example below, MyFunction and MyVar are imported from MyModule and accessible by their names in the current script (no prefix required).
import sys sys.path.append('path to module') from MyModule import MyFunction, MyVar x = MyFunction(15, 10) print(x) print(MyVar)
The output of the above code will be:
25 1000
Using the dir() function
The dir() method in Python may be used to return a list of all the functions, variables, classes, and other objects declared in a specific module.
Example:
The dir() method is used to locate all of the functions, variables, classes, and other objects specified in MyModule.
import sys sys.path.append('path to module') import MyModule x = dir(MyModule) print(x)
The output of the above code will be:
['MyFunc', 'MyFunction', 'MyVar', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
Built-in Modules
In Python, there are various built-in modules that may be imported and used in the same way in the current script. For example, the sys module, which is a built-in module, was imported in previous sections to specify the path of other modules. The math module is used to determine the square root of a given integer in the example below.
import math print(math.sqrt(25)) print(math.sqrt(30))
The output of the above code will be:
5.0 5.477225575051661