In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, how we can use the alias to rename the module, etc.
What is Python Module
A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.
Create a simple Python module
Let’s create a simple calc.py in which we define two functions, one add and another subtract.
Import Module in Python
We can import the functions, and classes defined in a module to another module using the import statement in some other Python source file.
When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches for importing a module. For example, to import the module calc.py, we need to put the following command at the top of the script.
Syntax of Python Import
Note: This does not import the functions or classes directly instead imports the module only. To access the functions inside the module the dot(.) operator is used.
Importing modules in Python
Now, we are importing the calc that we created earlier to perform add operation.
The from-import Statement in Python
Python’s from statement lets you import specific attributes from a module without importing the module as a whole.
Importing specific attributes from the module
Here, we are importing specific sqrt and factorial attributes from the math module.
Import all Names
The * symbol used with the from import statement is used to import all the names from a module to a current namespace.
from module_name import *
From import * Statement
The use of * has its advantages and disadvantages. If you know exactly what you will be needing from the module, it is not recommended to use *, else do so.
Locating Python Modules
Whenever a module is imported in Python the interpreter looks for several locations. First, it will check for the built-in module, if not found then it looks for a list of directories defined in the sys.path. Python interpreter searches for the module in the following manner –
- First, it searches for the module in the current directory.
- If the module isn’t found in the current directory, Python then searches each directory in the shell variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of directories.
- If that also fails python checks the installation-dependent list of directories configured at the time Python is installed.
Directories List for Modules
Here, sys.path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search for the required module.
[‘/home/nikhil/Desktop/gfg’, ‘/usr/lib/python38.zip’, ‘/usr/lib/python3.8’, ‘/usr/lib/python3.8/lib-dynload’, ”, ‘/home/nikhil/.local/lib/python3.8/site-packages’, ‘/usr/local/lib/python3.8/dist-packages’, ‘/usr/lib/python3/dist-packages’, ‘/usr/local/lib/python3.8/dist-packages/IPython/extensions’, ‘/home/nikhil/.ipython’]
Renaming the Python module
We can rename the module while importing it using the keyword.
Syntax: Import Module_name as Alias_name
Python built-in modules
There are several built-in modules in Python, which you can import whenever you like.
5.0 3.14159265359 114.591559026 1.0471975512 0.909297426826 0.87758256189 0.234143362351 24 3 0.401533172951 88.4917616788 True 1461425771.87