Python Modules
Python Module is essentially a python script file that can contain variables, functions, and classes. Python modules help us in organizing our code and then referencing them in other classes or python scripts.
A file containing Python definitions and statements is called a python module. So naturally, the file name is the module name which is appended with the suffix .py. For better understanding let’s create a python module to explore it completely. First create a file named printNumbers.py with the following contents.
#print 1 to n
for i in range(n):
print(i+1)
#print n to 1
for i in range(n):
print(n-i)
Now in the python interpreter import this module with the following command;
import printNumbers
This import command will look for printNumbers.py file in the current directory and PATH variable locations. Once the file is found, the code in the file will be available for us to use. Now to access the module’s function we need to use the module name like below:
Sometimes if the module is big, to ease the function calling we can rename the import like below:
Importing specific functions
Sometimes it’s unnecessary to import all the functions of a python module. We may need only one or two functions. In that case, we can use the following variant of the import statement;
One thing to notice here, as we import printForward, it is included in the current symbol table. So we don’t need to call the function like – printNumbers.printForward() Another variant can be useful sometimes. Here we used renaming as we did previously to ease our use of the function.
Also if we want to import all the names that a module defines there is another variant for importing. This imports all names except those beginning with an underscore (_). But this is not ideal practice as this introduces an unknown set of names into the interpreter.
FAQs on Python Modules
What are the built-in modules in Python?
There are a lot of built-in modules in Python. Some of the important ones are – collections, datetime, logging, math, numpy, os, pip, sys, and time. You can execute help(‘modules’) command in Python shell to get the list of available modules.
What is the difference between module and package in Python?
Python package is a collection of python modules. Python module is a single python file whereas python package is a directory having multiple python scripts and __init__.py file defining the package details.
Where can I find the Python Modules List?
You can find the list of Python modules from their official page of Python Module Index. However, if you are looking for the Python modules available to you, then you can execute help(‘modules’) command in Python shell to get the list of available modules.
How can I import a module from the different directory?
When we try to import a python module, it’s looked into the current directory and the PATH variable location. So if your python file is not present in these locations, then you will get ModuleNotFoundError. The solution is to import sys module and then append the required directory to its path variable. Below code shows the error when we try to import from a different directory and how I am fixing it by adding its directory to the path variable.
$ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'test123'
>>> import sys
>>> sys.path.append('/Users/pankaj/temp')
>>> import test123
>>> test123.x
10
>>> test123.foo()
foo
>>>