Python - Modules
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.
Create a Module
A module can be created with any name with a file extension .py.
Example:
The example below 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
Use a Module
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 their names with prefix module-name followed by a dot (.).
Example:
In the example below, 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). See the syntax below. MyFunc() function from MyModule is used in the current script with 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 imported module can be renamed using as keyword. 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 (.).
Example:
In the example below, the module MyModule is imported in the current script with a new name MMod. After that, MyFunc() function from MyModule is accessed in the current script with syntax MMod.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, 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. 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.
Example:
In the example below, 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('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 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.
Example:
The dir() is used to find out all the functions, variables, or classes etc defined 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
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. In the example below, math module is imported to calculate square root of a given number.
import math print(math.sqrt(25)) print(math.sqrt(30))
The output of the above code will be:
5.0 5.477225575051661