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 myModule.py.

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
print(myModule.student())
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 myModule.py file.

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

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

from myModule import totalStudents
print(totalStudents())
printing-specific-function-using-from-import.

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

from myModule import totalStudents
print(student())
from-import-statement-error.

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
print(my.student())
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 *
print(student())
print(totalStudents())
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.

Python
 └── mainFolder
     └── df.py

sys.path.append()

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 df.py prints hello!

#df.py file
print("hello!")

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.

sys.path.insert()

A similar alternative is to insert the path of df.py 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
sys.path_.insert-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.

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__))
PARENT_DIR_PATH = os.path.dirname(CURRENT_DIR_PATH)
sys.path.append(PARENT_DIR_PATH)
relative-directory

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
print(myModule.myList)
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')
helpmodules-function

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))
operator-module-examples

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]
print(choice(numbers))
random-module-example

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 *
print(sqrt(9))
print(factorial(5))
math-module-in-python

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
incorrect-module-name

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 df.py without providing a full path.

path-error-in-no-name-module

In the above example, you can see, the df.py 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
print(numpy.__doc__)
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.

complied-modules

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:

my_project
 ├── globalVariables.py
 ├── main.py
 └── myModule.py
  • 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.py – main program module

First, globalVariables.py file content:

def initialize():
    global number 
    number = 10

Here’s content for myModule.py file:

import globalVariables
def main():
    globalVariables.number+=10

The content of main.py file:

import globalVariables
import myModule
 
if __name__ == "__main__": 
    globalVariables.initialize() 
    print( globalVariables.number ) # print the initial value 
    myModule.main() 
    print( globalVariables.number )
get-access-to-global

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
print(dir(myModule))
dir-function-printing

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
print(dir())
dir-without-argument

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

reload(module)

Python version <= 3.3

import imp
imp.reload(module)

Python version >=3.4

import importlib
importlib.reload(module)

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 __init__.py. Every directory inside the Python path containing a file named __init__.py, 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_project
 ├── main.py
 └── my_package
     ├──__init__.py
     └── my_module.py

The content of main.py:

from my_package import my_module
print(my_module.test())

The my_package/__init__.py file is empty.

The content of my_package/my_module.py is:

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

Now, let’s execute main.py:

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
pip-list-to-display-packages.

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.

Summary

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.