Difference between Absolute & Relative Imports in Python
At the point when you work on a major Python project, you will partition the Python code into numerous Python records. This parting of code into various records gives a particular look to the undertaking, and it is probably the most ideal way to compose clean code. Generally, all the top Python libraries are composed utilizing different Python records known as Python modules and live in numerous indexes or organizers known as Python packages.
The Python import statement makes it easy for Pythonistas to use the code of one Python file in another. However, still many Python beginner and intermediate learners find the import statement confusing and do not know the difference between relative and absolute imports in Python (absolute vs relative imports).
Import Statement Syntax
Names of modules and packages are case-sensitive, and thus, make sure you are writing the correct name.
import module_name
or
import package_name
However, Python also provides the from
a statement that works along with import
. This makes the import statement sophisticated.
from package_name import module
or
from module_name import module_function
Example
#import module
>>> import math
>>> num = 34.88
>>> math.floor(num)
34
#from module import module function
>>> from math import floor
>>> num =35.9
>>> floor(num)
35
#from module import all functions or submodule
>>> from math import *
>>> num =48.37
>>> ceil(num)
49
Types of Python Imports
1. Python Absolute Imports
Absolute imports are generally used when we only want to import specific functions or sub-modules from modules or packages.
Syntax
└── project
| ├── package_A
| │ ├── __init__.py
| │ ├── module_A.py
| │ └── module_B.py
| └── package_B
| ├── __init__.py
| ├── module_C.py
| ├── module_D.py
| └── subpackage_A
| └── module_E.py
For instance, if you want to access a method function_E
that is written inside the module_E,
you need to specify the absolute path to module_E.py
and grab the function_E
method. For example:
from project.pakage_B.subpackage_A.module_E import function_E.
Using the same syntax, you can access any module and package of the project.
2. Python Relative Imports
Syntax
└── project1
| ├── package_A
| │ ├── __init__.py
| │ ├── module_A.py
| │ └── module_B.py
| │ └── CurrentProgram.py
| └── package_B
| ├── __init__.py
| ├── module_C.py
| ├── module_D.py
| └── subpackage_B
| └── module_E.py
|------project2
So, instead of using the absolute import, it would be a great practice to use the relative import because CurrentProgram.py
and module_B.py
are in the same location. In relative import, we use the (.) dot notation to represent the current directory.
#CurrentProgram.py
from .module_B import function_B
Access the module_E
method function_E
from module_C
.
#package_B/module_C
.subpackage_B.module_E import function_E
Absolute vs Relative Imports in Python
Conclusion
That was all about absolute vs relative imports in Python. Generally, Python developers prefer absolute import when they are working on shared or big projects. Because there, they want to make sure that other developers could also get the full path of the import module.