Relative imports in Python 3 are a way of importing modules and packages that are related to the current module or package. To create reusable code and make projects more manageable, Pythons allows you to organize code into packages and modules. To reuse code, you might need to make use of imports. In this article, we will learn relative imports in Python.
We will cover how to use relative imports to import modules from the same package, different packages, parent packages, and submodules.
To illustrate this topic we have the following project structure, which we will use throughout the article:
Myproject/
│
├── main.py
│
├── package1/
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
│
├── package2/
├── __init__.py
├── module3.py
└── module4.py
1. What Are Relative Imports?
Relative imports in Python 3 are a way of importing modules that are located in the same package or subpackage as the importing script. This is done by using a special syntax that indicates the relationship between the importing module and the module being imported.
Relative imports are used in place of absolute imports when you want to import a module that is located in the same package as the importing script. Relative imports are particularly useful for organizing code and avoiding naming conflicts.
2. When Should You Use Relative Imports?
In Python, there are two important ways of importing a package, module, or function. One is absolute import and the other is relative import. Both of these will work, however, there is a situation where the relative imports might be good rather than using the absolute imports.
- When working with smaller packages and modules that are closely related to one another
- When working with a simple package hierarchy that includes only a few levels of sub-packages
These are the two ideal cases where relative imports might be a good fit rather than using the absolute imports.
3. Relative Import Module from the Same Package
Refer to the structure of our project. Let’s say we are working in module1.py
and we want to import module2.py
. We can use a relative import to accomplish this.
In our case, To import module2.py
in module1.py
, we can use the following import statement:
# module1.py
from . import module2
3.1 Relative Import a Function
We can also use relative imports to import a specific function from a module within the same package. Suppose module1.py
contains a function called print_hi()
, and we want to import it in module2.py
. We can use the following relative import statement:
# module2.py
from module2 import print_hi
print_hi()
While with absolute import we must have to mention the full path:
# Absolute import
# module1.py
from Package_1.module2 import print_hi
# Call the function
print_hi()
3.2 Relative Import of a Module
Relative imports can be used to import another module within the same package. Suppose we want to import module1.py
in module2.py
. We can use the following import statement:
# module2.py
from . import module1
In this case, many people are confused, when they run this code and get the error saying “attempted relative import with no known parent package”. This error occurs because they execute the file as a python script and not as a python module. So you need to execute this as a python module.
# Run the code on Terminal
python -m Package.module
Here -m
stands for module.
4. Relative Import Module from Different Packages
You can use relative imports to import modules from a different package. When importing modules from a different package, we need to use a relative import that specifies the relative path of the package and module we want to import.
Referring back to the project structure, let suppose we are working in module1.py
and we want to import module3.py
from package_2
. We can use a relative import to accomplish this. We need to specify the relative path of module3.py
from module1.py
. In this case, module1.py
and module3.py
are in different packages, so we need to use two periods (..) to go up one level and then specify the name of the package and module we want to import.
To import module3.py
in module1.py
, we can use the following relative import statement:
# module1.py
from ..package_2 import module3
Remember if you execute this is python python code is a script you will get an error, so you will need to execute it using terminal.
4.1 Relative Import a Function
Suppose module3.py
contains a function called bar()
, and we want to import it in module1.py
. We can use the following relative import statement:
# module1.py
from ..package_2.module3 import bar
4.2 Relative Import a Module from a Different Sub-package
For example we want to import module3.py
in module2.py
. We can use the following relative import statement:
To import module3.py
in module1.py
, we can use the following relative import statement:
# module1.py
from ...package2 import module3
5. Relative Import from Sub Module
Importing modules from the same package, or a different package, we can also use relative imports to import modules from a sub module within the same package.
See the following project structure:
Myproject/
│
├── main.py
│
└── package1/
├── __init__.py
├── module1a.py
├── module1b.py
└── subpackage/
├── __init__.py
└── module1c.py
If we are working in module1c.py
in subpackage
and we want to import module1a.py
from the same package. We can use a relative import to accomplish this. We need to specify the relative path of module1a.py
from module1c.py
.
In this case, we can use two periods (..) to go up one level to the parent package, and then specify the name of the module we want to import.
To import module1a.py
in module1c.py
, we can use the following relative import statement:
# module1c.py
from .. import module1a
6. Summary and Conclusion
In this article, we have learned how to use relative imports in python 3. We have learned how to import modules, function within the same package and different packages. I hope this article was helpful. If you have any questions, leave them in the comment section.
Happy Coding!
Related Article
- Python Count Specific Character in String
- Python Not Equal Operator With Examples
- Python List Unpacking with Examples
- Python Tuple Comparison
- Negative Index of List in Python
- How to find if directory exists in Python
- Import Files from Different Folder in Python
- Python reversed() Function with Examples
- Python Add Two Lists By Index Wise
- Sort using Lambda in Python