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.

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 with the following contents.

def printForward(n):

    #print 1 to n
    for i in range(n):

def printBackwards(n):

    #print n to 1
    for i in range(n):

Now in the python interpreter import this module with the following command;

import printNumbers

This import command will look for 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

Sometimes if the module is big, to ease the function calling we can rename the import like below:

Python Modules

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 Modules

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 Modules

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

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 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

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

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.

Python Modules
Python os module
Python sys module
Python time
Python MySQL
Python CSV
Python multiprocessing
Python pickle
Python time sleep
Python queue
Python unittest
Python socket
Python SimpleHTTPServer
Python json
Python signal
Python random
Python System Command
Python Daemon Thread
Python Copy
Python threading module
Python struct
Python logging
Python subprocess
Python argparse
Python functools
Python itertools
Python getopt
Python ftp
Python tarfile
Python lxml
Python ConfigParser
Python datetime
Python decimal module
Python collections
Python zipfile
Python pdb
Python io
Python fractions
Python AST
Python HTTP
Python xmltodict
Python gzip
Python HTML Parser
Python inspect module
Python Send Email
Python tempfile
Python SQLite
Python shutil
Python timeit
Python getpass module
Python urllib
Python pytz
Python pendulum
Python arrow module


Leave a Reply

Your email address will not be published. Required fields are marked *