Python’s behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules. The PYTHONPATH variable holds a string with the name of various directories that need to be added to the sys.path directory list by Python. The primary use of this variable is to allow users to import modules that are not made installable yet. Let’s try to understand the concept using an example.
Suppose you defined a module as below:
Now if you attempt to import the_my_module.py in your python script as below:
This will lead to the following error:
This is due to the reason that PYTHONPATH is not set yet. In simpler words, the Python interpreter cannot find the location of my_module.py file. So to set PYTHONPATH on a windows machine follow the below steps:
Step 1: Open your This PC (or My Computer) and write click and click on properties.
Step 2: After the properties window pop up click on to Advance System Settings:
Step 3: Now click on the environment variable button in the new popped up window as shown below:
Step 4: Now in the new Environment Variable dialog box click on New as shown below:
Step 5: Now in the variable dialog box add the name of the variable as PYTHONPATH and in value add the location to the module directory that you want python to check every time as shown below:
Step 6: Now open your command prompt and execute the my_script.py file with the below command:
The behavior of python is set now so it should have no problem executing the python script by importing the my_module.py as shown below:
- How to setup Anaconda path to environment variable ?
- Python Virtual Environment | Introduction
- Creating Python Virtual Environment in Windows and Linux
- Python | Add packages to Anaconda environment
- Create virtual environment using venv | Python
- Set up virtual environment for Python using Anaconda
- Using mkvirtualenv to create new Virtual Environment - Python
- Python - Setting up the Bokeh Environment
- Environment Variables in Python
- Set up Opencv with anaconda environment
- Environment setup for CherryPy
- Environment Setup for Matplotlib
- Setup OpenCV With PyCharm Environment
- Python | Set 6 (Command Line and Variable Arguments)
- __name__ (A Special variable) in Python
- Different ways to access Instance Variable in Python
- Python | Using variable outside and inside the class and method
- Python | Variable list slicing
- Python | Check if a variable is string
- Python | Convert 1D list to 2D list of variable length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.