Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Import from parent directory

Python – Import from parent directory

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, Lazyroar.py file and in the subdirectory, we have a python file named temp.py, Now let’s try if we can import the Lazyroar module in the parent directory from the temp.py file in the subdirectory.

Lazyroar.py (module in the parent directory)

Python3




def geek_method():
    print("This method in Lazyroar module.......bye")


temp.py (python file in subdirectory)

Python3




# importing the module in
# parent directory
from parentdirectory import Lazyroar
 
# calling the func1() method
# from Lazyroar module
Lazyroar.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 Lazyroar

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.

Python3




import sys
 
# setting path
sys.path.append('../parentdirectory')
 
# importing
from parentdirectory.Lazyroar import geek_method
 
# using
geek_method()


Output:

This method in Lazyroar 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.

Python3




import path
import sys
 
# directory reach
directory = path.path(__file__).abspath()
 
# setting path
sys.path.append(directory.parent.parent)
 
# importing
from parentdirectory.Lazyroar import geek_method
 
# using
geek_method()


Output:

 This method in Lazyroar 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.

Python3




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 Lazyroar
 
 
Lazyroar.geek_method()


Output:

RELATED ARTICLES

Most Popular

Recent Comments