CARVIEW |
- Log in to:
- Community
- DigitalOcean
- Sign up for:
- Community
- DigitalOcean

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.
Python Modules
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:
Importing specific function of a Python Module
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
Let’s look into some commonly asked questions related to 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.

Please check this GitHub Repository for a list of most important python modules and learn them through their specific tutorials and example programs.
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
>>>
Python Modules List
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.
About the author
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Still looking for an answer?
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
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
- Table of contents
- Python Modules
- FAQs on Python Modules
- Python Modules List
Deploy on DigitalOcean
Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.
Become a contributor for community
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
DigitalOcean Documentation
Full documentation for every DigitalOcean product.
Resources for startups and SMBs
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Get our newsletter
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
The developer cloud
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Get started for free
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.