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.
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
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
<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:
- Add the sys and os.path modules.
- Using os.path.dirname(os.path.abspath("__file__")), set directory to the parent directory of the currently open file.
- Using sys.path, add the parent directory to the Python path.append(os.path.dirname(os.path.dirname(directory))).
- Use the from Favtutor import my_function command to import the my_function function from the Favtutor module.
- 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.
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 :)