There are multiple packages already in the python packages which are loaded in Python by default. Sometimes it is essential to know how to import module from parent directory in python in order to use particular methods and features that might not be available in Python by default. This is generally used by people in an organization where particular techniques that are exclusive to be used by the organization members are shared with the team members.
In this article, we’ll see how to import module from parent directory in python and go about multiple methods to do the same and any hacks that are possible to work our way around the same.
What is a module in Python?
A module is a small Python file that contains some python code and has statements and definitions within it. The file itself can be called with a module name with a .py extension. The file can have some exclusive code that you can use to make your analysis simpler. The code breakdown into smaller parts can help a python programmer take their efforts into the analysis without having to repeat the entire basic definitions of the code that can be imputed from another module in python.
Modules are generally imported using the import keyword in python, which can be used within a statement to import modules that are predefined in Python or another module that exists in the parent directory as you will see in the section titled, “How to import module from parent directory in Python?”
Check out this link to understand more: Modules in Python explained
What is a Parent Directory in Python? (and for coding languages in general)
A directory can be considered to be a storage area or a vault with multiple layers within it. The parent directory is the outermost layer that encompasses all the layers within it and the layers inside it are other directories within the parent directory. The working directory is where the programming language currently is executing commands and the general default working directory is at the path of installation in the system.
Check out the article mentioned at the beginning of this article to understand how you can change your working directory in Python.
How to import module from parent directory in Python?
Let us go over three methods that you can use to import a module from the parent directory in Python. Feel free to use any of the mentioned codes and copy them as required. These are pulled from resources like StackOverflow and Python docs to help you find the different methods in one single place.
The first and foremost method which you can use is to add the file path as an argument in the sys.path.append() function which will add the parent directory into the default workspace or the PYTHONPATH of your python environment and you can then import your required modules from there without any problem. This is by far, the simplest method to retrieve or import module from parent directory in python.
Find an example execution of the code here:
import sys # appending the parent directory path sys.path.append('parent directory path here') # importing the methods from parentdirectory.parent_module import parent_directory_method parent_directory_method()
and now you can use the methods in your environment with the parent directory being appended using the sys.path command.
When you don’t know what the file path to your parent directory is, then you can make use of the os library in python and get the path of your current working directory and then check the directory name using the os library and continue on with replacing the file path with the variable containing the output from the os library i.e. parent_directory in this case and import the required module from the parent directory.
import sys import os current = os.path.dirname(os.path.realpath(__file__)) parent_directory = os.path.dirname(current) sys.path.append(parent_directory) from parentdirectory.parent_module import parent_directory_method parent_directory_method()
Another simple way is to use the sys.path.insert along with the os library’s abspath command which looks like this:
import os, sys parent = os.path.abspath('.') sys.path.insert(1, parent) import parent_directory_module
Using the parent directory with the ‘.’ as done in the Command Line Prompt or the Anaconda prompt can help you with the os.path.abspath code to retrieve the parent directory to work with. The parent directory’s path is then inserted using the sys.path.insert code into the PYTHONPATH environment and thereby allowing you to work with the modules in your parent directory as and how required. Check out this YouTube video if you think a video tutorial with the same can help you better.
Krish Naik is one of the best data science tutors out there and in this video, he explains how you can work around Python and import modules in different directories in your python environment in an easy way. The directories are created from scratch and is easier to understand in the video tutorial and better to follow along with.
Generally, most data scientists today have a pre-prepared format of analysis procedure to follow in their projects. Such lines of code generally fall under the category of the EDA or Exploratory Data Analysis. Having a module named eda.py can speed up such time-taking processes by letting data scientists focus more on the outcome of the code and avoid repetitiveness of tasks that can be cloned from another project.
It is in these cases that the requirement to understand how to import modules from parent directory in python comes into the picture and is essential for data scientists to know, in order to ensure that the modules that are prepared in advance for such scenarios find their way to the projects and save time and efforts.
Try importing a module from your own parent directory into your projects and let us know if you think there is a better method for “How to import module from parent directory in Python?” in the comments below!
For more such content, check out our website -> Buggy Programmer