Python modules

Python Modules

A module is a file that contains a working Python code. It can have various statements, variables, functions, etc. stored in it.

The file has a name and a file extension type of .py, this is the native file type of Python, and this file can be stored locally on your computer or on the cloud. 

Modules have very significant advantages that we shall discuss later in this article. The process of creating modules is called modularizing.

Creating Modules

The term “module” with relation to Python is maybe new to you, but believe it or not, you have already created several modules while coding. 

As discussed already, modules are nothing but a normal code that you write in Python. When you write Python code in an Integrated Development Environment (IDE), it automatically saves that code in the file type of .py.

Modules are used to break down a long Python code into smaller parts, so you can store your most important classes and functions in a separate file and import it into your main code. To import a module, first, let us write some code and save it.


Python import module save

The code does not print anything because no function has been called.

Functions have a great advantage of separating your main code into different blocks, but because modules exist, you can store all of the functions in a different file.

To import the code above, save this code in a file named (Although you can choose a different name for your module, if you want to follow this tutorial with ease, save the file with the same name). Now, it is time to use the module we created.

If you have no clue in which directory your current code is saved, an existing module in Python called OS can help you. It is a module that contains functions used to interact with your machine’s operating system.

The OS module has a function getcwd() that returns the directory where your current code is being executed.


Python getcwd function

Now we have got our directory where the python file is saved. The rest of the task is going to be easier now.

Import Python Modules

Create a new “” file in the same directory where you saved the file. This file will be used to import the module that we created. Let us understand how it is done.


Python import modules

How is the code above working?

  • When we execute this file, the code line import computer tells Python to find and use the file named and copy all of the functions inside it to the current program.
  • To access the functions in the module, you have to use the dot (.) separator.

If Python does not find the module, it will give us an error called “ModuleNotFoundError”.

Importing Specific Functions

The method we used above makes every function in the module available to us, but what if we want to import only a single function? 

from module_name import function_name


Python import specific functions

The from keyword tells Python to search for the function we specified in the computer module. It gets imported only if it is found.

By following this approach, we can avoid using the dot separator again and again. If you have a good idea about what functions you want to use from a module, then it will drastically reduce your size of the code.

You can import multiple functions in a single line by separating each function name using a comma.


Python import multiple functions

As you can see, both the functions got imported.

Renaming Modules and Functions

If you have a component with the same name as the module or function that you are importing or if the name of the module is too long to type again and again, the import keyword gives you the option to rename modules and functions.

import module_name as new_name


Python random Module

Above, we have tried to simulate the output of dice when it is rolled. The randint() function generates a pseudo-random number between the specified parameters. 

Important Tips:
There are many pre-installed modules in Python with lots of functions, before using any module, you should always read the documentation of that module and functions inside it. For example, there is a module in Python named “math” that contains all of the important mathematics functions.

Useful Module Functions

You can also assign a same value to multiple variables in the same line of code.

The reload() function

Python imports a module only once in a session. Here is a quick demonstration of it:

Let us create a module that adds two numbers and prints them.


Python add numbers

After saving this module as, it is the time to import it in our main code file.


Python import main code

You can see that our module did not get used again after the first execution.

Python does this to keep the process efficient but what happens if we make some changes in our module, would the module in the main code also get refreshed? The answer is no, Python will continue using the old version of the module.

There are two ways to solve this issue:

  1. Restart the interpreter.
  2. Use the reload() function in the importlib

No one would like to restart their interpreter as Python interpreters take a bit of time to restart. So, we should use the second approach, i.e., to use a function inside the importlib module.


Python reload function importlib module

This function can be used anywhere in the code but it is recommended to reload your modules before executing the functions and classes as it would remove any chances of error that could have existed on the older module.

The dir() function

This module function returns sorted strings of names that are defined in the module, for example, let us use this function on the built-in Python math module.


Python dir function

You can see that there are various functions and classes defined inside the math module. 

The modules that have underscores behind and in front of them, e.g., __name__ or __doc__ are the string variables that contain the name of the module or the docstring for the module respectively.


Python underscore modules

The dir() function is a good way to have a quick look on a module and its functions.

Import Every Function in a Module

All of the function from a module can be imported at once.

from function_name import *

When Python executes this code, the asterisk tells the language to read the whole module and import every function.


Python import all functions

Important Tips:
Importing all of the functions from a module is a bad practice. You should never import functions from modules that are not written by you. There is a possibility that Python might overwrite existing functions in your code with the same name.

While importing from unknown modules, the best way to import functions is by importing them one by one using the dot separator or importing functions directly from modules.

Advantages of using Modules

  • Reusability: One of the biggest importance of Python language is that we can write reusable code in it. Modules only make the reusability of Python just better by creating a separate file for the code that can be emailed to your friend or a team member.
  • Simplicity: Problems get solved faster when you divide the problem into separate chunks of smaller problems. The main function of modules is to make the code simpler and to divide the whole problem into smaller parts.
  • Maintenance: While working in large organizations, a whole team of developers works on a single project and as a team member you have to write modules according to the job assigned to you.
    A module can be edited and more functions can be added to it, this reduces the possibility that a mistake of an individual might affect the whole team. 

Tutorials for all brains!