Python Import from parent directory

6 min read

Python’s import system allows developers to make use of functionality defined within other modules in order to enhance their projects. By default, however, we can only carry out import if the modules we seek are in the same directory as the running module. What if we want to import from a different directory? Specifically, How can we import from the parent directory in Python?

In this article, we will explore how we can do this in Python. We will briefly explore the import system, and then solve the problem of importing modules from a parent directory. So, let us begin!

Understanding the Directory Structure in Python

Before we delve into the topic, let us first see the directory structure we are going to be working with. The directory structure is shown below:

directory structure

From the above figure, we can see that we have a main directory (kind of a root directory) named parent_directory. This directory contains another sub-directory current_directory, and a Python module named module2.py. The sub-directory, current_directory, contains a module named module1.py. It is from module1.py that we will be doing our imports from module2.py in the parent_directory.

Now that we understand the directory structure we will be working with, let’s dive into the fundamental concepts of importing modules in Python. Understanding these concepts will provide a solid foundation for our exploration of importing modules from the parent directory.

Importing Modules in Python

Python provides the ability to import functionality from other modules using the import statement. Using this feature, we can access classes, functions, variables, and other resources defined in separate Python files or packages.

Using the import statement is quite straightforward. Let us examine the several variations of the usage of import. Each variation caters to a different requirement. Let’s explore some common import statement variations.

Importing the Entire Module in Python

The most basic usage of the import statement is by using the import keyword, followed by the module name. For example, the following snippet in the module we are importing from:

import module2

imports the entire contents of module2. Then in order to access any contents of the module, we make use of module2 as a prefix. For instance, consider the contents of module2:

def function1():
    # do something here

class class1:
    # add the class definition here

in order to import the function from module2, we will need the following code snippet:

import module2


Importing Specific Items from a Module in Python

Another usage case might be importing only some functionality from the target module. In order to do this, we need to do it as shown in the following example:

from module2 import function1, class1

In the above example, we import function1 and class1 from module2. With this syntax, we can directly reference function1 and class1 without prefixing them with the module name. See it demonstrated in the following example:

from module2 import function1, class1

result = function1()
instance = class1()

The provision of import capability allows us to program in a modular way. It allows us to separate code into modules based on functionality.

Now that we have demonstrated how to import from the same directory, it is time to explore how to carry out imports from a module contained in a parent directory and the challenges involved in doing so.

Python Importing from the Parent Directory

When working with projects having multiple directories, even with a nested structure, importing functionality from other directories might become necessary.

In our case, it might become necessary to import classes, functions, and variables from a module contained in a parent directory.

We are going to solve this challenge by considering the file structure in the previous figure.

The challenge is how we can import function1 and class1 from module2 in parent_directory into module1 found in child_directory.

We might think that the following  python code snippet will solve our problem:

from parent_directory import module2

Instead, we will get the following error message from Python:

ModuleNotFoundError: No module named 'parent_directory'

Also, if we try the relative imports:

from .. import module2

we will also get the following error message:

ImportError: attempted relative import with no known parent package

So, how do we resolve to import from parent_directory?

PYTHONPATH environment variable

We got the previous error messages from Python because Python checked an environment variable named PYTHONPATH, and did not find a path to the module we needed to import. PYTHONPATH contains the paths that Python will search for modules during import. It is a way of telling Python where to look for any modules we need to import.

So, the key to importing from parent_directory is to somehow update the environment variable PYTHONPATHwithin module1 , the module we want to import the functionality from.

Python Import from the parent directory using sys.path

We can modify the PYTHONPATH by considering that sys.path also contains the list of directories to look within when searching for modules. To make import from parent_directory possible from module1 in child_module, we need to add the path to parent_directory to sys.path. Here’s an example using a relative path to the parent directory:

import sys

parent_dir = ".."

import module2


In order to make use of sys.pathmethod, we have to first import the sys module. The rest is pretty straightforward; we just append the relative path ".." to the sys.path list. Now, Python does not have any errors because it can locate module2.

Alternatively, we can append the absolute path to parent_directory by using the following example:

import os
import sys

child_dir = os.path.dirname(__file__)
parent_dir = os.path.abspath(os.path.join(child_dir, '..'))

In this example code, we made use of the functionality provided by the os module to get the absolute [path to parent_directory, before appending it to sys.path. Specifically,

  • We used os.path.dirname(__file__) to get the absolute path to child_directory. The variable __file__ stores a reference to the current module (in this case module1.py)
  • Then, we constructed a relative path to parent_directory by calling os.path.join(child_dir, '..'), and finally,
  • We used os.path.abspath to get the absolute path from the relative path we just constructed.

Once we get the absolute path to parent_directory, we can append it using sys.path before carrying out the import.

That wraps it up for this article on how to import modules from a parent directory in Python. We did a quick refresher on how to import modules on the same directory.

Then we talked about how to modify the environment variable PYTHONPATH in order to be able to import from other directories. We then finally showed how modifying sys.path can help us import modules from another directory, especially the parent directory.

Thanks for reading, and check out other articles in our knowledge base to learn more about Python.