Python Import from Parent Directory in Simple Way

In Python, to use different functions in our code, we usually import different modules.

Importing modules saves every programmer from creating redundant code or implementing the same functions in other files.

How does Python Import from Parent Directory?

In Python, you must first set a path to the system’s absolute path before you can import from a parent directory.

Meanwhile, to import a file in Python, use the import statement.

When importing from a parent directory in Python, set the path to the system’s absolute path

Wherein we can import modules from where we are currently working.

However, we cannot import modules from the parent directory.

However, we can import the child directory by giving the name of the current module, then a dot, and then the name of the child module.

Below is the hierarchy of the files that are stored in a directory.

Import Module from Parent Directory

Let’s say we want to import a module from the parent directory of the current project directory.

3 Ways to Import from Parent Directory in Python

There are three different ways to do this, thus are explained below.

1. Import a Module from the Parent Directory in Python Using the Relative Import

Using the current directory path as a point of reference, the relative import is used to import a module into the code.

We import a module into the code using relative import by referring to the current directory path.

Then, we add the __init__.py file to the parent directory to make the directory a package.

We use the import statement to bring in a module.

After the parent directory has been configured as a package, we can use the relative package method to import the module.

Directory Tree:

parentdirectory
├── main.py
├── parent-file-sample.py
├── parent-file-sample2.py
├── __init__.py
└── childdirectory
    ├── child-file-sample.py
    ├── chile-file-sample2.py
    └── __init__.py

Let’s say, for example, that child-file-sample.py is the current directory.

The example code given below shows how to import a module from its parent package.

from ..parentdirectory import sample_module

To import the module from the directory two levels above the current one, we need to put three dots before the package directory name to go back to two levels, as shown in the code below.

from ...rootdirectory import sample_module

Should I use relative or absolute imports in Python?

The preferred way to import in Python is through absolute imports because they are clear and easy to understand.

By looking at the statement, you can tell exactly where the imported resource is.

Moreover, absolute imports are still valid even if the location of the import statement moves.

2. Import a Module From the Parent Directory in Python by Adding It to PYTHONPATH

The PYTHONPATH environment variable specifies the list of directories from which Python should import modules and packages.

Therefore, if we include the parent directory from which the module must be imported, Python will automatically search the parent directory for the appropriate module.

3. Using the sys.path.insert() Method

Another method is using sys.path.insert() to add the parent directory to the sys.path list. The sys.path is the list of strings that specifies the paths to find packages and modules.

The PYTHONPATH environment variable’s directory list is stored in the sys.path file. The sys.path.insert() method can be used to add other paths.

In Python, you can use the sys.path.insert() method to add the parent directory to the sys.path list.

The code below shows how to demonstrate this.

import os, sys

path_abs = os.path.abspath('.')
sys.path.insert(1, path_abs)

import sample_module

Summary

This Python tutorial explained the different methods of how Python import from the parent directory.

If you found the information in this article useful, please consider sharing it with others who may find it useful as well.

What method of Python import do you prefer? Let us know in the comment section below.

Leave a Comment