Relative imports in python

If you have ever built a project that requires you to put your codes in different python files, you surely have had to go through building modules and packages. This generally helps in better maintenance for a large codebase.

Well, before trying to understand how relative imports work, we should first be clear about what are modules and how are they different from packages.

Modules

Any Python file is a module, its name being the file's base name without the .py extension.

Module Vs Package

A package is a collection of Python modules: while a module is a single Python file, a package is a directory of Python modules containing an additional __init__.py file, to distinguish a package from a directory that just happens to contain a bunch of Python scripts.

Now Let's understand how are they used.

Import

Whenever we want to use an in-built python module we have to use an `import` statement. For e.g. the statement

import string

allows us to use the functions defined inside the string module.

From the python-documentation regarding module, we see that:

When a module named string is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named string.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).

  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • The installation-dependent default.

This just means that at first, it tries to do an absolute import. For relative imports, we should use a different strategy, especially if the name of our module is the same as one of the built-in-module.

Relative Import

Let's consider the following file structure for one of our packages

pkg
├── __init__.py
├── main.py
└── string.py

To import the module, string.py inside main.py, we have to use the import statements in the form:

from . import string

In case, we need to import a function foo  from the string.py module, we can use

from .string import foo

 

Additional leading periods(.) perform the relative import starting from the parent of the current package. For example, code in the X.Y.Z module can do:

# Imports A.B.D
from . import

# Imports A.E
from .. import E

# Imports A.F.G
from ..F import G

We hope this helps you in solving your problems regarding relative imports since they can be really confusing. Learning the different insights about python can be tough at times, we intend to make your learning curve smoother.

Till we meet again in another blog, Keep Hacking.

0 Comments

Join discussion:

Login