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.
def printForward(n):
#print 1 to n
for i in range(n):
print(i+1)
def printBackwards(n):
#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:
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.
Let’s look into some commonly asked questions related to Python modules.
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.
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.
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.
Please check this GitHub Repository for a list of most important python modules and learn them through their specific tutorials and example programs.
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
>>>
There are thousands of Python modules and more are getting developed every day. We have written tutorials for a lot of popular Python modules. Just follow the links from the below table to learn these modules.
References:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Hi Sir, Really you have a lot of patience and very well explained …Thanks for all your efforts. And it is helpful for many folks like me.
- Sarah
Hi Sir, You are doing a wonderful job! I don’t have words. My self Sathish, I am a beginner of python. I am searching so many python web sources no one gave the subject like you anyway finally I got your web page it’s very easy you explained very well. I appreciate. Thank you, sir. Regards, Sathish
- sathish