As the program gets longer, it can be split into several files for easier maintenance. A file can be created containing all necessary functions that can be included in several programs of the application without actually copying it into each program. Such a file is called module.
A module can be created with any name with a file extension .py.
The below example shows the code of Module called MyModule. Please note that the file extension of the module must be .py. In this modules two functions called MyFunc and MyFunction are created. MyFunc takes no argument and prints Hello World!. while MyFunction takes two numbers as arguments and returns sum of those numbers. Along with this, it also contains variable called MyVar.
def MyFunc(): print("Hello World!.") def MyFunction(a, b): return a+b MyVar = 1000
To use a module, it must be imported in the current script using Python keyword import followed by module name. If the module is kept in the same folder as the current script, it will be imported with the mentioned statement otherwise its path must be shown in the current script. After importing the module, all functions, variables, and classes etc defined in the module can be used in the current script using thier names with prefix module-name followed by a dot (.).
The the below example, the above mentioned module MyModule is imported in the current script. The module is kept in another folder, therefore, the path of the module is configured using one of the commonly used method (using sys.path.append function after importing sys module). Please see the syntax below. MyFunc() function from MyModule is used in the current script with syntax MyModule.MyFunc().
import sys sys.path.append('/var/www/html/') import MyModule MyModule.MyFunc()
The imported module can be renamed using as keyword. Please see the example below for syntax. After renaming, all functions, variables, and classes, etc defined in the module can be used in the current script using their names with prefix new name followed by a dot (.).
The the below example, the module MyModule is imported in the current script with new name MMod. After that, MyFunc() function from MyModule is accessed in the current script with syntax MMod.MyFunc().
import sys sys.path.append('/var/www/html/') import MyModule as MMod MMod.MyFunc()
Instead of importing the whole module, the specific parts of the module like specific function, variable, or class, etc can be imported in the current script using from and import keywords. Please see the example below for syntax. In this method, the imported parts can be used in the current script using their names with no prefix.
The the below example, MyFunction and MyVar are imported from MyModule in the current script which are accessed using their names (no prefix required).
import sys sys.path.append('/var/www/html/') from MyModule import MyFunction, MyVar x = MyFunction(15, 10) print(x) print(MyVar)
The Python built-in dir() function can be used to return a list containing all the functions, variables, or classes etc defined in a given module.
The dir() is used to find out all the functions, variables, or classes etc defined in MyModule.
import sys sys.path.append('/var/www/html/') import MyModule x = dir(MyModule) print(x)
['MyFunc', 'MyFunction', 'MyVar', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
There are several built-in modules in Python which can be imported and used in the current script in the same way. For example - In previous sections sys module which is a built-in module is imported to configure the path of other modules.