Locating Python modules
After encountering the import
statement, the Python interpreter searches the module in the following sequence:
- The current directory, which contains the running script. I advise you, be sure, that the name of your program is different from the importing modules.
PYTHONPATH
has to be defined in the environment variable in Windows OS. Let's learn by example. Consider we move themod5.py
program to another directory as covered in the following screenshot.
But the current directory does not contain any module1
module. The output will be as shown here:
Output of mod5.py in a different directory
We are running the mod5.py
program from F:project_7daysmodulepack
. The output shown there is no module1
module found. Let's set the PYTHONPATH
variable as shown here:
Setting the PYTHONPATH variable
After setting PYTHONPATH
, you will have to reboot your computer. After reboot, start the program again.
The output will be as shown here:
This is the output of the program after setting PYTHONPATH...