Skip to main content
HomeTutorialsPython

Working with Modules in Python

Modules enable you to split parts of your program in different files for easier maintenance and better performance.
Updated Jul 29, 2024  · 8 min read

As a beginner, you start working with Python on the interpreter. Later when you need to write longer programs you start writing scripts. As your program grows in size, you may want to split it into several files for easier maintenance as well as reusability of the code. The solution to this is modules. You can define your most used functions in a module and import it, instead of copying their definitions into different programs. A module can be imported by another program to make use of its functionality. This is how you can use the Python standard library as well.

Simply put, a module is a file consisting of Python code. It can define functions, classes, and variables and can also include runnable code. Any Python file can be referenced as a module. A file containing Python code, for example: test.py, is called a module, and its name would be test.

There are various methods of writing modules, but the simplest way is to create a file with a .py extension, which contains functions and variables.

Before you start writing your own modules, I recommend you take a look at our Intro to Python for Data Science course, which covers the basics of the Python programming language.

Learn Python From Scratch

Master Python for data science and gain in-demand skills.
Start Learning for Free

The import Module in Python

To use the functionality present in any module, you have to import it into your current program. You need to use the import keyword along with the desired module name. When the interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot (.) operator along with the module name. First, let's see how to use the standard library modules. In the example below, the math module is imported into the program so that you can use the sqrt() function.

import math             #You need to put this command,`import` keyword along with the name of the module you want to import
num = 4
print(math.sqrt(num))   #Use dot operator to access sqrt() inside module "math"

For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter, if it’s just one module you want to test interactively, use reload(), for example: reload(module_name).

Writing Modules in Python

Now that you have learned how to import a module in your program, it is time to write your own, and use it in another program. Writing a module is just like writing any other Python file. Let's start by writing a function to add/subtract two numbers in a file calculation.py.

def add(x,y):
    return (x+y)
def sub(x,y):
    return (x-y)

If you try to execute this script on the command line, nothing will happen because you have not instructed the program to do anything. Create another Python script in the same directory with the name module_test.py and write the following code into it.

import calculation            #Importing calculation module
print(calculation.add(1,2))   #Calling function defined in add module.

If you execute module_test.py, you will see "3" as output. When the interpreter came across the import statement, it imported the calculation module in your code and then by using the dot operator, you were able to access the add() function.

Additional Ways to Import Modules in Python

There are more ways to import modules:

  • from .. import statement
  • from .. import * statement
  • renaming the imported module

from ... import statement

The from..import statement allows you to import specific functions/variables from a module instead of importing everything. In the previous example, when you imported calculation into module_test.py both the add() and sub() functions were imported. But what if you only needed the add() function in your code? Here is an example to illustrate the use of from..import.

from calculation import add
print(add(1,2))

In the above example, only the add() function is imported and used. Notice the use of add()? You can now access it directly without using the module name. You can import multiple attributes as well, separating them with a comma in the import statement. Take a look at the following example:

from calculation import add,sub

from ... import * statement

You can import all attributes of a module using this statement. This will make all attributes of imported module visible in your code.

Here is an example to illustrate the use of from .. import *:

from calculation import *
print(add(1,2))
print(sub(3,2))

Note that in the professional world, you should avoid using from..import and from..import*, as it makes your code less readable.

Renaming the Imported Module in Python

You can rename the module you are importing, which can be useful in cases when you want to give a more meaningful name to the module or the module name is too large to use repeatedly. You can use the as keyword to rename it. The following example explains how to use it in your program.

import calculation as cal
print(cal.add(1,2))

You saved yourself some typing time by renaming calculation as cal.

Note that you now can't use calculation.add(1,2) anymore, as calculation is no longer recognized in your program.

Module Search Path in Python

You may need your modules to be used in different programs or projects and their physical location in the directory may be different. If you want to use a module residing in some other directory, you have some options provided by Python.

When you import a module named calculation, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named calculation.py in a list of directories given by the variable sys.path. sys.path contains these locations:

  • The directory containing the input script (or the current directory)

  • PYTHONPATH (a list of directory names with the same syntax as the shell variable PATH)

  • The installation-dependent default

Assume module_test.py is in the /home/datacamp/ directory, and you moved calculation.py to /home/test/. You can modify sys.path to include /home/test/ in the list of paths, in which the Python interpreter will search for the module. For this, You need to modify module_test.py in the following way:

import sys
sys.path.append('/home/test/')

import calculation
print(calculation.add(1,2))

Byte Compiled Files in Python

Importing a module increases the execution time of programs, so Python has some tricks to speed it up. One way is to create byte-compiled files with the extension .pyc.

Internally, Python converts the source code into an intermediate form called bytecode. It then translates this into the native language of your computer and then runs it. This .pyc file is useful when you import the module the next time from a different program - it will be much faster since a portion of the processing required for importing a module is already done. Also, these byte-compiled files are platform-independent.

Note that these .pyc files are usually created in the same directory as the corresponding .py files. If Python does not have permission to write to files in that directory, then the .pyc files will not be created.

The Python dir() Function

The dir() function is used to find out all the names defined in a module. It returns a sorted list of strings containing the names defined in a module.

import calculation
print(test.add(1,2))
print(dir(calculation))

Output:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add', 'sub']

In the output, you can see the names of the functions you defined in the module, add & sub. Attribute __name__ contains the name of the module. All attributes beginning with an underscore are default Python attributes associated with a module.

Conclusion

Creating a module is required for better management of code and reusability. Python provides you with some built-in modules, which can be imported by using the import keyword. Python also allows you to create your own modules and use them in your programs. It gives you byte-compiled files to overcome the cost of using modules, which makes execution faster. You can use dir() to know the attributes defined in the module being used in a program. This can be used both on pre-defined modules as well as user-defined modules.

If you would like to learn more about Python, you can take a look at our Intermediate Python for Data Science course.

Topics

Python Courses

Course

Introduction to Python

4 hr
5.8M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related
Data Skills

blog

6 Python Best Practices for Better Code

Discover the Python coding best practices for writing best-in-class Python scripts.

Javier Canales Luna

13 min

tutorial

Working with Zip Files in Python

In this tutorial, you are going to learn how to work with Zip Files in Python using the zipfile module. zipfile is a Python built-in module.
Hafeezul Kareem Shaik's photo

Hafeezul Kareem Shaik

13 min

tutorial

String Split in Python Tutorial

Learn how you can perform various operations on string using built-in Python functions like split, join and regular expressions.
DataCamp Team's photo

DataCamp Team

2 min

tutorial

How to Document Python Code

Learn why there is a need for documenting code and best practices to do it. Further, learn to leverage the potential of the Pydoc module for documenting purposes.
Aditya Sharma's photo

Aditya Sharma

14 min

tutorial

Pip Python Tutorial for Package Management

Learn about Pip, a powerful tool that helps you properly manage distribution packages in Python.
Javier Canales Luna's photo

Javier Canales Luna

11 min

tutorial

Reading and Editing PDF’s and Word Documents From Python

This tutorial will allow you to read PDF documents and merge multiple PDF files into one PDF file. It will also show how to read and write word documents from Python.

Olivia Smith

8 min

See MoreSee More