Python Absolute vs Relative Imports

Python – import:

Import in Python is identical to C/C++’s include header files. Python modules can access code from another module by using import to import the file/function. The import statement is the most commonly used method of initiating the import machinery, although it is not the only method. The import statement includes the import keyword as well as the name of the module.

The import statement does two operations: it searches for a module and then binds the search result to a name in local scope. When you import a module, Python executes all of the code in the module file and makes it available to the importer file.

When a module is imported, the interpreter first looks for it in sys.modules, which is a cache of all previously imported modules. If it is not found, it examines all built-in modules with that name; if it is found, the interpreter runs all of the code and saves it to a file. If the module is not found, it looks for a file with the same name in the list of directories specified by the sys.path variable.

sys.path is a variable that includes a list of paths to Python libraries, packages, and the input script’s directory. If a module named math is imported, the interpreter looks for it in the built-in modules; if it is not found, it looks for a file named math.py in the list of directories specified by sys.path.

Absolute Import

When we import modules using absolute importing, we must specify the module’s complete path following the import keyword.

The whole path comprises the main module/library as well as any submodules or functions that will be used in the program.

For Example:

└── My_project
    ├── package_1
    │   ├── My_module1.py
    │   └── My_module2.py
    └── package_2
        ├── __init__.py
        ├── My_module3.py
        ├── My_module4.py
        └── subpackage_1
            └── My_module5.py

There is a directory called My_project, which has two subdirectories called package_1and package_2. My_module1.py and My_module2.py can be found in the package_1directory.

There are three files in the package_2directory: two modules, My_module3.py and My_module4.py, and an initialization file, init .py. It also includes a subdirectory called subpackage_1, which contains a file called My_module5.py.

Let us consider the following:

  • package_1/My_module2.py contains a function, function1.
  • package_2/__init__.py contains a class, class1.
  • package_2/subpackage_1/My_module5.py contains a function, function2.

Practical Implementation:

from package_1 import My_module1
from package_1.My_module2 import function1
from package_2 import class1
from package_2.subpackage_1.My_module5 import function2

Advantages and Disadvantages

Advantages:

  • Absolute imports are quite helpful because they are concise and to the point.
  • Even if the current position of the import statement is modified later, the absolute import statements remain valid.
  • Absolute import may be determined simply by glancing at the statement and determining the location of the imported resource.

Disadvantages:

Absolute imports are ineffective when the directory structure is large. In this scenario, relative imports work fine.

Assume we need to import a function from a higher-level module in the root module. The absolute import command for such a module would look like this:

from packg_1.subpackg_2.subpackg_3.subpackg_4.module_5 import functn_6

If the desired function is present any farther down in the layers, things will become quite complex and difficult.

Relative Import

The relative technique allows us to import a function relative to the location of a specific package or subpackage, making it a more efficient method of importing modules and functions.

We have two types of relative importing ways, implicit and explicit, but the implicit strategy no longer works with Python3.x versions.

The relative importing method is depicted below:

from .subpackg_a.module_5 import functn_4

Advantages and Disadvantages

Advantages:

  • The relative import statements are short and to the point.
  • It lowers the complexity of an import statement based on current location.

Disadvantages:

  • This technique is difficult to read, as the programmer is unaware of numerous root modules.
  • The programmer has no idea where the function we’re importing came from.

Bullet-point Differences:

                         Absolute Import                       Relative Import
In absolute import, we supply the module’s full path from its root directory or package.In relative import, we supply the path to the module’s location from the currently running or active Python program or script.
Absolute imports are usually lengthy.Usually, relative imports are short.
Absolute imports are easier to read and understand, i.e., more clearThese imports are less clear and a bit tough to read.