Tutorial

Python Modules

Published on August 3, 2022
Default avatar

By Pankaj

Python Modules

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.

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: python modules, python module import example Sometimes if the module is big, to ease the function calling we can rename the import like below: python module import example function

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; python tutorial, python import module function using from 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. python tutorial, python modules import variant with from and as 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. python modules tutorial, module import variant 3 with from and star

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.

Python Modules List
Python Modules List

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.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 5, 2020

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

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 2, 2019

    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

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel