In this article, we will learn ways to import modules from the parent directory in python.
Python has lots of in-built modules to import methods and functions to add functionalities to our code.
However, python also let us define our own methods and import them as a module in our codes.
Here, we will see different ways to import methods from the parent directory to our sub-directories as modules in python.
Suppose we have a file structure as described below :
Directory Tree of the Project
📦py-demo ┣ 📂current_dir ┃ ┗ 📜sum_function.py ┗ 📂parent_dir ┃ ┣ 📜addition.py ┃ ┗ 📜__init__.py
Here, we have a current directory (current_dir) where we will run our python code and we have a parent directory (parent_dir) from which we will import the functions for our code.
We have a simple
add function to add two numbers in the
def add(x,y): return x+y
We will import this
add() function to our
Now to use the import statement to import function from the parent directory, we have to add the
__init__.py file inside it as shown in the above file structure tree.
__init__.py makes python treat the directory containing the file as a module.
__init__.pyis an empty file.
Now, we have done declaring the parent directory as a module.
Let’s see different ways to import from the parent directory in python.
Import module from Parent directory using sys.path.append() method
sys module provides functions and variables to manipulate different parts of the Python runtime environment.
sys.path is an in-built variable of python that list all the directories from which the python interpreter searches for the required module.
sys.path.append() function lets the user add a specific path for the python interpreter to search.
So to import from the parent directory we have to use the
sys.path.append() function to add the path of the parent directory. This will let the python interpreter search for the imported module in that directory.
Example: (In sum_function.py)
import sys sys.path.append('.') from parent_dir.addition import add print(add(2,2)) # 4
Here, we have imported the
add() function from the
addition module located in the
We have passed the path of the parent directory in the
sys.path.append() function as a parameter.
Import from parent directory using relative sys.path.insert() method.
sys.path contains a directory list in the
PYTHONPATH environment variable that the python interpreter search for when looking for a module.
To add the parent directory for our code, we have to use the
sys.path.insert() method as shown below.
import sys sys.path.insert(1,'.') from parent_dir.addition import add print(add(1,1)) # 2
Here, we have imported the module
addition from the parent directory in the
sum_function.py file to add two numbers using the
sys.path.insert(pos, path) takes in two parament- first the position in the directory list and second is the path of the directory we want to add.
Import from parent directory using os module
os is an in-built module in python that provides various functionalities to change files and directories.
We can use the
os.path.abspath() function to get a normalized absolute version of the parent directory pathname that will help us to import modules from the parent directory of our project.
To get the normalized absolute path we just have to pass the path of the directory as a parameter.
import sys import os parent_directory = os.path.abspath('.') sys.path.append(parent_directory) from parent_dir.addition import add print(add(1,1)) # 2
Here, in the sum_function.py file, we have used the
os.path.abspath() function to get the absolute pathname of the parent directory.
Next, we used the
sys.path.append() function to add it to the list of python directories that the python interpreter uses to search for modules.
Here, we have learned how to treat a directory as a module in python using
__init__.py file and how to import these modules using
os.path.abspath() methods from the in-built sys and os python modules.
Other Articles You’ll Also Like: