Python – Import from parent directory
Last Updated :
21 Aug, 2024
In this article, we will learn how to Import a module from the parent directory. From Python 3.3, referencing or importing a module in the parent directory is not allowed, From the below example you can clearly understand this.

In the parent directory, we have a subdirectory, geeks.py file and in the subdirectory, we have a python file named temp.py, Now let’s try if we can import the geeks module in the parent directory from the temp.py file in the subdirectory.
geeks.py (module in the parent directory)
Python
def geek_method():
print("This method in geeks module.......bye")
temp.py (python file in subdirectory)
Python
# importing the module in
# parent directory
from parentdirectory import geeks
# calling the func1() method
# from geeks module
geeks.geek_method()
As we have discussed earlier it is not possible to import a module from the parent directory, so this leads to an error something like this.
Traceback (most recent call last):
File “C:/Users/sai mohan pulamolu/Desktop/parentdirectory/subdirectory/temp.py”, line 2, in <module>
from parentdirectory import geeks
ModuleNotFoundError: No module named ‘parentdirectory’
Now let’s learn how to import a module from the parent directory:
In order to import a module, the directory having that module must be present on PYTHONPATH. It is an environment variable that contains the list of packages that will be loaded by Python. The list of packages presents in PYTHONPATH is also present in sys.path, so will add the parent directory path to the sys.path.
For our work, we use three different approaches that are explained below with the help of examples.
Method 1: Import from parent directory using sys.path method
Here we will use the sys module and set the path directly to the required module.
Add the parent directory to the sys.path using the append() method. It is a built-in function of the sys module that can be used with a path variable to add a specific path for interpreters to search. The following example shows how this can be done.
Python
import sys
# setting path
sys.path.append('../parentdirectory')
# importing
from parentdirectory.geeks import geek_method
# using
geek_method()
Output:
This method in geeks module.......bye
Method 2: Import from parent directory using os.path.abspath() method
Here we will use the sys module as well as the path module for getting the directory and set the path directly to the required module.
Syntax: os.path.abspath(path)
Parameter:
Path: A path-like object representing a file system path.
Return Type: This method returns a normalized version of the pathname path.
Firstly we will get the name of the directory where the temp.py file is presently using the path.path(__file__).abspath(), secondly add the directory to the sys.path.append to check, we will use its method.
Python
import path
import sys
# directory reach
directory = path.path(__file__).abspath()
# setting path
sys.path.append(directory.parent.parent)
# importing
from parentdirectory.geeks import geek_method
# using
geek_method()
Output:
This method in geeks module.......bye
Method 3: Import from parent directory using os.path.dirname method
Here we will use the sys module as well as the os module for getting the directory (current as well as a parent) and set the path directly to the required module.
Syntax: os.path.dirname(path)
Parameter:
path: A path-like object representing a file system path.
Return Type: This method returns a string value which represents the directory name from the specified path.
Firstly we will get the current directory by using the os.path.dirname(os.path.realpath(__file__)), secondly, we will get the parent directory by using the os.path.dirname(), finally, add the parent directory to the sys.path to check, we will use its method.
Python
import sys
import os
# getting the name of the directory
# where the this file is present.
current = os.path.dirname(os.path.realpath(__file__))
# Getting the parent directory name
# where the current directory is present.
parent = os.path.dirname(current)
# adding the parent directory to
# the sys.path.
sys.path.append(parent)
# now we can import the module in the parent
# directory.
import geeks
geeks.geek_method()
Output:

Similar Reads
Python - Import from sibling directory
In this article, we will discuss ways to import files from the sibling directory in Python. First, create two folders in a root folder, and in each folder create a python file. Below is the dictionary tree: Directory Tree: root : | |__SiblingA: | \__A.py | |__SiblingB: | \__B.py In B.py we will crea
3 min read
Python â Import module from different directory
While working on big projects we may confront a situation where we want to import a module from a different directory. But for some reason, the module may not be imported correctly. Now donât worry if your module is not imported correctly. In this article, we will discuss ways to import a module fro
4 min read
Python - Import module outside directory
Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module into that file. If the module exists in the same directory as the file, we can directly import it using the syntax import
4 min read
Python Directory Management
Python Directory Management refers to handling and interacting with directories (folders) on a filesystem using Python. It includes creating, deleting, navigating and listing directory contents programmatically. Python provides built-in modules like os and os.path and the newer pathlib module, for t
5 min read
Get parent of current directory using Python
In Python, OS module is used to interact with the operating system. It comes under Python's standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system. OS modu
4 min read
Python - List Files in a Directory
Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python. Table of Content What is a Directory in Python?How to List Files in a Directory in Python
9 min read
__import__() function in Python
__import__() is a built-in function in Python that is used to dynamically import modules. It allows us to import a module using a string name instead of the regular "import" statement. It's useful in cases where the name of the needed module is know to us in the runtime only, then to import those mo
2 min read
Python Circular Imports
The Circular Imports in Python programming occur when two or more modules mutually depend on each at the same time forming a loop between them. These modules' dependency on each other often results in the program getting stuck and generating circular import errors. What does the Python Circular Erro
4 min read
Python | os.DirEntry.is_file() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.scandir() method of os module yields os.DirEntry objects corresponding to the
3 min read
Import Modules From Another Folder in Python
In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two
2 min read