Absolute vs Relative Imports in Python

Absolute Imports

You've found a workable pace on the best way to compose import explanations and how to style them like a master. Presently it's an ideal opportunity to get familiar with somewhat more with regards to outright imports.

A flat out import determines the asset to be imported utilizing its full way from the venture's root organizer.

Syntax and Practical Examples
Let’s say you have the following directory structure:

└── project
    ├── package1
    │   ├── module1.py
    │   └── module2.py
    └── package2
        ├── __init__.py
        ├── module3.py
        ├── module4.py
        └── subpackage1
            └── module5.py

There’s a directory, project, which contains two sub-directories, package1 and package2. The package1 directory has two files, module1.py and module2.py.

The package2 directory has three files: two modules, module3.py and module4.py, and an initialization file, init.py. It also contains a directory, subpackage, which in turn contains a file, module5.py.

Let’s assume the following in this Absolute vs Relative Imports in Python:

package1/module2.py contains a function, function1.
package2/init.py contains a class, class1.
package2/subpackage1/module5.py contains a function, function2.

The following are practical examples of absolute imports:

from package1 import module1
from package1.module2 import function1
from package2 import class1
from package2.subpackage1.module5 import function2

Pros and Cons of Absolute Imports

Outright imports are favoured in light of the fact that they are very clear and direct. It is not difficult to tell precisely where the imported asset is, just by taking a gander at the assertion. Moreover, outright imports stay substantial regardless of whether the current area of the import articulation changes. Indeed, PEP 8 unequivocally suggests outright imports.

Relative Imports

A general import determines the asset to be imported comparative with the current area—that is, the place where the import explanation is. There are two sorts of relative imports: certain and express. Implied relative imports have been belittled in Python 3, so I will not be covering them here.

Syntax and Practical Examples

The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:

from .some_module import some_class
from ..some_package import some_function
from . import some_class

You can see that there is something like one speck in each import proclamation above. Relative imports utilize spot documentation to indicate area.

A solitary spot implies that the module or bundle referred to is in a similar registry as the current area. Two dabs imply that it is in the parent catalog of the current area—that is, the registry above. Three dabs imply that it is in the grandparent catalog, etc. This will presumably be recognizable to you on the off chance that you utilize a Unix-like working framework!

Let’s assume you have the same directory structure as before:

└── project
    ├── package1
    │   ├── module1.py
    │   └── module2.py
    └── package2
        ├── __init__.py
        ├── module3.py
        ├── module4.py
        └── subpackage1
            └── module5.py

You can import function1 into the package1/module1.py file this way:

# package1/module1.py

from .module2 import function1

You’d use only one dot here because module2.py is in the same directory as the current module, which is module1.py.

You can import class1 and function2 into the package2/module3.py file this way:

# package2/module3.py

from . import class1
from .subpackage1.module5 import function2

In the first import statement, the single dot means that you are importing class1 from the current package. Remember that importing a package essentially imports the package’s init.py file as a module.

In the second import statement, you’d use a single dot again because subpackage1 is in the same directory as the current module, which is module3.py.

Conclusion

Great job for coming to the furthest limit of this brief training on outright and relative imports! Presently you're up to speed on how imports work. You've taken in the accepted procedures for composing import proclamations, and you know the distinction among outright and relative imports.

39