What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Import File from Parent Directory in Python? (with code)

  • Apr 18, 2023
  • 5 Minute Read
How to Import File from Parent Directory in Python? (with code)

Working on a lengthy project with a lot of recurring functionalities? Looking for a simple method to modify your code? Your issues can be resolved because Python is a dynamically large language that gives you plenty of options for customizing and condensing your lengthy codes.

Utilizing the split and code features, i.e. For each function you need to use, you can create a separate file and then just call the function 'n' times in your main code. All files must be in the same parent directory, and that is the only consideration. In this article, we will learn how to import modules or files from the parent directory.

How to Import Files from the Parent Directory in Python?

Yes, you can import things from the parent directory in Python. This can be achieved using the following method.

You can create the function that you will later need to import when creating the program in an empty file, then save the file with the extension ".py". The Python file will now be converted into a Python module, which you can then import into any other file by simply executing the command "import 'filename'". Just remember that both files need to be in the same directory.

Code:

import my_function 

 

Output:

Hello

 

Directory:

python parent directory

As you can see, we have a my_function.py file that contains a print statement. When we import it into a fresh Jupyter notebook, it simply returns the output "Hello," indicating that the module has been imported properly. But there are more methods as well.

The best way to import files from the parent directory in Python is by using the sys module. The sys module offers access to several variables that the interpreter uses or maintains, as well as to functions that have close relationships with the interpreter.

The path variable, which is a list of strings that provides the search path for modules, is one of these functions. You can import files and modules from the parent directory by adding the path of the parent directory to this list.

Either you can import from parent directory using sys.append() method, as shown below:

import sys
sys.path.append('..')
from Favtutor import my_function

 

Output:

Hello

 

Here, we've used the append() function to add the parent directory path to the system path before importing the 'my_function.py' file.

Or you can import from the parent directory using os.path.abspath() method, with the following code:

import os.path
import sys

# directory reach
directory = os.path.dirname(os.path.abspath("__file__"))
 
# setting path
sys.path.append(os.path.dirname(os.path.dirname(directory)))

# importing
from Favtutor import my_function
my_function

 

Output:

<module 'Favtutor.my_function' from 'f:\\projects\\Favtutor\\..\\Favtutor\\my_function.py'>

 

In order for Python to locate and import the my_function function from the Favtutor module, this code adds the parent directory of the current file to the Python path.

The code is broken down as follows:

  1. Add the sys and os.path modules.
  2. Using os.path.dirname(os.path.abspath("__file__")), set directory to the parent directory of the currently open file.
  3. Using sys.path, add the parent directory to the Python path.append(os.path.dirname(os.path.dirname(directory))).
  4. Use the from Favtutor import my_function command to import the my_function function from the Favtutor module.
  5. Use my_function to invoke the my_function function.

Python 3 and Python 2 both allow for the import of files and modules from the parent directory.

If you still have any doubts, you can chat with our online python tutors to understand this better.

Conclusion

In conclusion, using the sys module, it is feasible to import files and modules from the parent directory in Python. These methods make managing complex projects in Python with many subdirectories simple. Happy Learning :) 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Komal Gupta
I am a driven and ambitious individual who is passionate about programming and technology. I have experience in Java, Python, and machine learning, and I am constantly seeking to improve and expand my knowledge in these areas. I am an AI/ML researcher. I enjoy sharing my technical knowledge as a content writer to help the community.