To write a module in Python, all you need to do is create a Python file and write some code to it.
For example, to create a module called
example, create a file called
example.py and add the following function to it:
def greet(): print("Hello from a module!")
There is your first Python module. To use it, you need to import the module into another file.
For example, let’s create another file called
call.py and let’s use the
greet() function from the
import example example.greet()
Hello from a module!
And there you have it. You just created a module and used it in another Python file.
To take a deeper look at how to work with modules in Python, read along.
Modules in Python
What Is a Module
A module is a Python file with code.
There are loads of built-in modules, such as the math module. But then there can also be some user-defined modules that you as a developer have created to structure your code.
A file called
example.py is a module whose name is
Modules are useful when you want to break down big programs into smaller and manageable units of code.
Say you have a program where you define the same functions in different files. Instead of writing duplicated functionality, you should create a separate module. This way you can write the necessary functionality only once, and use it all over your codebase.
Before showing how to import functionality from a modules in Python, I’m going to show you how to create a module in Python.
Let’s create a module with three functions and a constant. Let’s call this module
To do this, create a
utilities.py file and add the following code to it:
FAVORITE_NUM = 1000 def greet(): print("Hello from a module!") def add(a, b): return a + b def bye(): print("Bye bye!")
Here is your first module. Now you can start using it in different parts of your project.
How to Import Modules for Use
To use a module, you need to include it in the file where you want to use it. This happens with the import statement. Notice that there are many ways you can import the module or parts of it:
- Import the module name
- Import a module and rename it.
- Import only some specific functionality from the module.
- Import everything from the module.
Let me show how each of these works.
1. Import the Module Name
To import a module, use the following:
For example, let’s import the
utilities module to a file (in the same folder) called
Now you can use the code of the
utilites module in the
test.py file using the dot notation:
utilities.add(1, 2) # returns 3
You cannot call the functionality directly without the module name because now only the module name is imported—not the functionality. But with the module name, you can access the functionality of the module.
2. Import and Rename a Module in Python
Repeating the name of the module can be tedious. Especially if the name is long or if you have to do it over and over again. To remedy this, you can create a shorthand for the module as you import it.
Let’s continue with the
If you don’t want to repeat the word
utilities, just rename the module as you import it.
This is easy:
import utilities as ut ut.add(1, 2) # calls the utilities.add() function
3. Import Specific Functions or Variables from a Module in Python
You may also import specific names from a module without importing the whole module.
For instance, let’s grab the FAVORITE_NUM from the
from utilities import FAVORITE_NUM print(FAVORITE_NUM)
4. Import Everything from a Module in Python
Then last (and least) you can import everything from a module using an asterisk
For example, let’s import everything from the module
from utilities import *
Now you can directly call any function or variable of the module without the module name:
print(FAVORITE_NUM) greet() add(1, 2) bye()
Even though this seems the easiest way to include modules, it is bad practice. It sacrifices the code readability. It can even lead to accidental duplicate definitions for a function for example. So prefer not doing it this way.
How to Import Modules from Different Folder
If you want to include a module from another folder, use the dot notation.
For example, let’s import the
add() function from the
example module to a file called
test.py. But this time the module is found in a folder called
├── test.py └── modules └── example.py
To do this, use the dot notation to access the module inside of a folder:
from modules.example import add
If this does not work for you, please remember to add an
__init__.py file into the folder of your module. This file can be empty, but it has to be there.
Today you learned how to create a module in Python.
To recap, a module is a Python file with statements and definitions. To use a module, you need to import it into your Python file.
Thanks for reading. I hope you find it useful.