Python Modules – Complete Tutorial

A Python module is a file containing the definition of a set of functions, statements, and variables. It can also contain runnable code too. Moreover, modules allow us to split our program into different parts for better performance and help to bring better code structure to the program. In this article, we will discuss Python modules and packages, how to create, and import modules, and review some of the most important packages in Python.

Creating Module in Python

Creating Modules in Python is very easy. All you need is to create a new file with .py extension.

See the example below. We created a function that returns ‘present’ and saved the file as

def student():
    return "Present"
Creating module in Python

Importing a Module in Python

To get access to the module’s functionality, we need to import it into our current program. Importing a module in Python is very simple. Open the text editor, and use the keyword import to import your module. 

As we already created our module with the name myModule, now let’s import it from another Python file.

See the example below to understand it in more detail.

import myModule
Importing Python Module

Now, let’s take a look at how we can use the functions that are defined in modules.

import myModule
Importing and using modules

The following is Python demonstrates how to use a function defined in the module; moduleName.functionName as shown in the above example.

Importing specific code from the Python module

In Python, modules allow you to import only a specific part of a module. This is achieved by using the from ... import statement. To illustrate this example, let’s add one more function to our file.

def student():
    return "Present"
def totalStudents():
    return 40

Now, let’s replace an import statement in the file to import only the totalStudents() function by using from keyword.

from myModule import totalStudents

Now, if we want to get access to student() function from our file, it would give us an error. Because we had only imported totalStudents() function using from keyword:

from myModule import totalStudents

The from import statement let us imports specific functionality of the module instead of importing the whole.

Using module alias in Python

You can set up a module alias for the module during its import. That allows you to import and use different modules with the same name in your Python program. To do that, you need to use import as statement. It works similarly to the import statement, but the module code is available for you through the provided alias. 

Let’s take a look at the example:

import myModule as my
Import as statement

Import everything from the module in Python

The import * statement in Python allows us to import all the functions and variables from the module except those starting from double underscores (__). Functions and variables that start with a double underscore are private and not accessible from the outside scope

Take a look at the following example to see how the import * statement works:

from myModule import *
Import everything from the module

The use of import * statement has some disadvantages. It often creates poorly readable code. So, be careful when using import * statements in your program.

Importing modules from another folder

In Python, there are many ways to import modules from another folder. Here we will discuss the two most common ways to import Python modules from another folder. 

Let’s suppose our module is located in the following folder.

 └── mainFolder


First, we have to import sys and then append the directory of the module in sys.path.

For example, see the below python code to understand the appending method.

Let’s say our prints hello! file

Now let’s import it through the sys.path.append() method.

import sys
sys.path.append("Full Path")
import df
Importing Python modules using sys.path.append method

Note: The hardcoding of the path is not the best practice and is used here for illustration purposes. We’ll show how to use the relative path in the next section.


A similar alternative is to insert the path of to position 1 of the system’s path variable. This ensures that it’s loaded with higher priority and avoids some naming conflicts. See the example below to understand the sys.path.insert() method.

import sys
sys.path.insert(1,'Full Path')
import df

Note: The hardcoding of the path is not the best practice and is used here for illustration purposes. We’ll show how to use the relative path in the next section.

Relative path

As we already mentioned, hardcoding of the path is a bad practice because it makes your code not portable. To fix this, you can use a relative path.

import os, sys
CURRENT_DIR_PATH = os.path.dirname(os.path.realpath(__file__))

In the example above:

  • __file__ – when a module is loaded from a file in Python, the __file__ is set to its path.
  • os.path.realpath(__file__) – returns a canonical path of the specified filename (in our case module)
  • os.path.dirname(os.path.realpath(__file__)) – will return a folder that contains a file (current module directory)
  • os.path.dirname(CURRENT_DIR_PATH) – will return a directory from a directory (parent module directory)

Now, you can use such statements to construct a path to any folder within your codebase.

Importing variables from the Python module

Apart from functions, a module can also contain variables of all types. For example, sets, tuples, dictionaries, and objects. Let us modify our myModule file and add a list and store it in a variable. See the example below to understand more clearly.

myList = [1, 2, 3, 4, 5]

Now let’s try to import this variable from another module:

import myModule
Importing variable from Python module

Built-in Modules in Python

Python has a large number of built-in modules. We can use these modules by importing them into our program. To display all the modules that are available in python, type help(‘modules’) in python IDE.

>> help('modules')

Examples of using built-in Python modules

Using operator module in Python

This module provides pre-defined functions for all the operators in Python. For example, mul(a, b) returns the product of two parameters. See the example below, which uses a couple of predefined functions from the operator module.

from operator import *
a = 10
b = 5
print(mul(a, b))
print(add(a, b))

Using the random module in Python

This module is used to create random numbers and values. Some of the predefined functions include randint(), choice() etc.

See the example below to see the functionality of randint() and choice function().

from random import *
print(randint(1, 10))
numbers = [1, 2, 3, 4, 5, 6]

The randint() function creates a random number from 1 to 10 and choice() function returns any one random element of the list.

Using math module in Python

The math module in python is used to perform mathematical operations. This module provides some predefined math functions. For example sqrt() function is to find square root and factorial() function is to find factorial.

from math import *

Advantages of code modularization

Splitting Python code into packages and modules has many advantages, for example:

  • Simplify code management – modules like functions allow you to reuse bigger chunks of code from various places within your program
  • Modules can be reloaded and rerun as many times as needed
  • Better program namespace management – you never get access to another module within a package unless you explicitly import it
  • Better code encapsulation – you can group related modules into a single unit called a package.

Python no name module error

Sometimes while importing modules, we can get ‘no module name.’ message. There can be several reasons for this error. Some of which we will cover in this section.

The name of the module is incorrect

The first and most common reason is the typo error. If we type the name of the module incorrectly, then this error appears. So, checking the module’s name before importing it is important.

In the example, let’s import a python library with an incorrect name and see what happens.

import Numpyy

The path to the module is incorrect

The second common reason for this error could be the incorrect path. This error will prompt if we try to import any module that is not in the same directory without providing a complete path.

Let’s try to import without providing a full path.


In the above example, you can see, the file exists but in another directory.

More on modules in Python

In this section, we will explore some advanced-level concepts related to modules in python.

Installing modules in Python

Python modules allow us to use the code of others in our program. This saves our time and makes our code readable. There are thousands of Python modules available, and we can install and use them in our programs.

We can install modules or packages with the Python package manager (pip). Open a terminal and use the pip command below to install a module.

pip install moduleName
Installing modules in Python

Docstrings in Python Modules

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. 

See the example of a docstring in python.

def main():
    '''This is main function'''

The docstring for Python Modules should list all the available classes, functions, objects, and exceptions that are imported when the module is imported. Moreover, it should also have a one-line summary of each item. Let’s import any module and print out its docstring to see the format.

import numpy
Printing module docstring

The module search path

If we want to use modules from a different directory rather than a current directory, Python provides a couple of ways to access such modules. 

When we import a module, the Python interpreter will first search the same name in the built-in modules directory. If the module is not found, then it searches the module in the list of directories provided by sys.path

sys.path contains the following directories:

  1. The directory of the input or current program
  2. PYTHONPATH –  a list of directory names, with the same syntax as the shell variable path

Compiled Python files

To speed up the loading of modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc, where the version encodes the compiled file format. It generally contains the Python version number.


Global variables in modules

Global variables in Python modules are a bit tricky to use. To be able to use global variables in modules, the following conditions should be met:

  • A module containing a global variable should be imported
  • A global variable should be initialized by calling a function where you’re defining a global variable

Let’s illustrate those conditions by example.

Example project structure:

  • globalVariables – module, which is responsible for defining and initializing all global variables.
  • myModule – this module modifies the global variable defined in the globalVariables module.
  • – main program module

First, file content:

def initialize():
    global number 
    number = 10

Here’s content for file:

import globalVariables
def main():

The content of file:

import globalVariables
import myModule
if __name__ == "__main__": 
    print( globalVariables.number ) # print the initial value 
    print( globalVariables.number )

Get information about modules in Python

The dir() is a built-in function used to determine which names the module defines. It returns a list of strings. 

See the below example to understand dir() function. This example shows dir() with argument.

import myModule
import sys

dir() function without argument returns the list of names we have defined currently.

See the example below to understand the dir() function without argument.

import myModule
import sys

Note that it returns all types of names (variables, functions, modules, etc.) but does not return built-in functions and variables.

Python reloading module

When we import a module, it is loaded only once per interpreter session. 

If we edit the module source file using an external editor and try out the new version, we have to restart our interpreter to load the new version. However, in Python, there is a built-in function reload() that reloads the module without restarting the interpreter. 

The following Python code is used to reload modules in different versions of Python.

Python version 2.x


Python version <= 3.3

import imp

Python version >=3.4

import importlib

When reload() is executed:

  • The Python module’s code is recompiled and the module-level code re-executed, defining a new set of objects bound to names in the module’s dictionary by reusing the loader that originally loaded the module. However, the init function of extension modules is not called again.
  • The old objects are only reclaimed after their reference counts drop to zero.
  • The names in the module namespace are changed to new objects if there exist any.
  • Other references to the old objects are not rebound to refer to the new objects and must be updated in each namespace where they occur if desired.

One of the advantages of reload function is that when a reload function is called, the dictionary containing the global variable is retained. Redefinitions of names will override the old definitions. Even if the new version of a module does not define a name defined by the old version, the old definition remains.

Packages in Python

We learned that modules are files containing Python statements, functions, and classes. The package is simply a folder of modules. In other words, the package is a directory with Python files and a file with the name Every directory inside the Python path containing a file named, will be treated as a package by Python. It’s possible to put several modules into a Package.

Packages are a way of structuring Python’s modules by namespace by using. For example, a module name X.Y means that the module named Y is referenced from the package X.

Let’s define a package by creating the following folder structure:

 └── my_package

The content of

from my_package import my_module

The my_package/ file is empty.

The content of my_package/ is:

def test():
    return "This is my_module test"

Now, let’s execute

packages and function

Listing all the Packages

To get a list of all the packages, open your IDE or Python terminal and type pip list. 

This will display all the packages along with their version. See the example below:

pip list

Install Python package or module

If we’re trying to import a module and Python tells us that it is not installed, we must use the pip command to install it first. So, before importing any module, ensure it is installed in your system or virtual environment.

pip install moduleName

Python modules vs. classes

Class and module are two separate things. A class can be instantiated, but a module cannot. A module is simply a library of different methods.  A class can be so much more; it can hold its state (by keeping track of instance variables) and be duplicated as often as you want. It’s all about objects. 

Moreover, classes in Python are templates for creating objects. They contain variables and functions which define the class objects. At the same time, modules are Python programs that can be imported into another Python program. Importing a module enables the usage of the module’s functions and variables in another program.

Python modules can contain classes, but Python classes can not contain modules.


In this article, we’ve covered Python modules, packages, and their imports and reviewed some of the most important packages in Python.

If you found this article useful, please, help us to spread it to the world.