Absolute and Relative Imports in Python

Many times when we create python code we find that we need to access code from another python file or package. This is when you need to import that other python file or package into your current code. So the straight forward way to achieve this is just written the below statement at the top of your current python program.

import package_name or module_name
from pacakge_name import module_name/object_name

When the above statement is parsed the interpreter does the following.

  • The interpreter will look for names in the cache of all modules that have already been imported previously. The name of this cache module in sys.modules.

  • If not found in the above step then the interpreter will search for it in a list of built-in modules. These modules are part of python standard libraries.

  • In case it is still not found in step-2 above, then the interpreter will search for the packages or module names in a list of directories defined in sys.path which has the current directory as the first directory to be searched.

  • On finding them to be an imported module in any of the above steps, the name of the package or module is bound to the local scope of the current program.

  • If the package or module is never found, then ModuleNotFoundError is raised.

Some rules about importing.

Import statements should be mentioned at the top of the files using those statements. The order in which the import has to be mentioned is as below.

  • Python's standard library modules

  • Import from third-party modules

  • Import from local applications

Absolute Import

In this type of import, we specify the full path of the package/module/function to be imported. A dot(.) is used in pace of slash(/) for the directory structure.

Consider the following directory structure for a package.


Also, Let's assume moduleA2 has a function names myfunc. When we want to import that function to our current python program, then using absolute path we mention the following import statement.

from packageA.moduleA2 import myfunc

The big advantage of absolute import is, it clearly indicates where the import is happening on the other hand sometimes it can get quite lengthy.

Relative Import

In relative import, we mention the path of the imported package as relative to the location of the current script which is using the imported module.

A dot indicates one directory up from the current location and two dots indicates two directories up and so on.

Consider the following directory structure for a package.


Let's assume moduleB1 in the above package structure needs to import moduleA1. Then the import statement is:

from ..packageA import moduleA1

The two dots indicate that from the location of moduleB1, we have to move to the directory python_project_name and then go to packageA to get moduleA1.

This kind of import are short and the top level project can be moved from one location to another without changing the path in the import statements easily. On the downside, if the import folders are shared then the code easily gets impacted when there is some modification in the path.

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started