The process of integrating a module from the parent directory will be discussed in this article. A package in the parent directory cannot be integrated or accessed as of Python 3.3 version.”
Example no 1
Let’s see when we will incorporate the geeks’ package in the parent directory from the temp.py folder in the subdirectory. The parent directory contains a new folder called geeks.py, and the subdirectory contains a data file termed temp.py.
At the start of the program, we have to import the “abc” framework from the parent directory. In the next step, we are going to call the function geek_method(). This function is related to the “abc” module.
After executing the above-mentioned program, we find an error “No module named parentdirectory”, as we previously defined that it is not feasible to integrate a module from the parent directory.
Solution of the Error “No module named parentdirectory”
Let’s now examine the process for integrating a package from its parent directory. The following steps must be followed and implemented in the system to fix the error.
Example no 2
A module’s directory needs to have been in the PythonPATH to be integrated. The environment variables that Python uses have a big impact on how it operates. PYTHONPATH has become one of those parameters. It’s being utilized to specify the user-defined packages’ integrate path so that they will be employed effectively in Python programs. It will be responsible for managing the main search directory of different packages.
A string containing a set of several directories that Python needs to append to the sys.path file and is stored in the PYTHONPATH variable. That variable’s main purpose is to enable users to integrate packages that have not yet been declared installable. Let us just use an illustration to try to better comprehend the point.
print("The user-defined package has been recently integrated")
We will define the function module(). Within this function, we utilize the print() method. This function prints the line. The user-defined package has been recently integrated.
Example no 3
If users want to incorporate the my_module() library, as shown below in the Python code.
my_module.module()
Now we have to incorporate the “my_module” header file. The module() method will be used in the following step. This function is taken from the my_module library.
We see the error “ModuleNotFoundError” after running the code above. This is because PYTHONPATH hasn’t been configured properly. To put it another way, the Python interpreter is unable to locate the my_module.py package. Therefore, adhere to the procedure below to configure PYTHONPATH on a Windows operating system:
Step 1
We are going to open the search bar of our system and type the control panel here.
Step 2
The control panel will be opened and appear on the screen just like this.
Step 3
When the control panel is opened, numerous options are displayed. These options allow you to modify the machine’s settings. We have to choose a system and security from among all of these possibilities. You can check this image for the verification of this step.
Step 4
After selecting system and security, a wide range of options are displayed. We will choose Advanced system settings from among all of these alternative options. You can check this image for the verification of this step.
Step 5
A box with various system properties displays on the screen when we choose the advanced system and security option. You can check this image for the verification of this step.
Step 6
In the newly opened panel, we now select the environment variable option. After selecting the environment variables feature, we get a menu as shown in the figure below:
Step 7
There are two types of variables shown on the screen, which include user variables for the user and system variables. We want to add a new variable in the “user variables for users” category, so we click on the “New” option. We get a dialogue box after clicking the “New” option. This dialogue box has two rows. The first row shows the Variable name, and the second row shows the Variable value. You can check this image for the verification of this step.
Step 8
Now, we’ll enter PYTHONPATH as the variable name in the pop-up window, and then we will enter the path of the framework directory that we can have to access the Python at every session as the variable value. Consequently, we enter the path “C:\Users\Administrator\Desktop” here. The set of modules that Python can acquire is stored within that environment variable. The parent directory information would be added because the set of modules in PYTHONPATH is already available in the path. You can check this image for the verification of this step.
Conclusion
First, in this article, we have executed a piece of code that resulted in an exception. This is due to the fact that in such a code, we would not import the parent directory. We have imported the parent directory to fix this error. The directory that contains the package needs to be on the PYTHONPATH. The collection of frameworks that Python may fetch is stored within the environment variable. Then, in the next code, the module() method is defined. Following the definition of this function, a new program is executed, and we have integrated the my_module header file in this code. Subsequently, we have been calling the my_module library’s module() method. We get the error when we run this code. However, we get a module-related exception this time. Then we followed some steps to get rid of this error. We will import the parent directory by going through this next step.