Environment variables influence the behavior of Python. PYTHONPATH is one such environment variable; that is, it is a key-value pair stored in a computer's memory. It is available to all programs running in the same environment.
Understanding the Pythonpath Environment Variable in
An environment variable that lets you add additional directories where Python looks for packages and modules. As these variables are not needed for Python to run, they are not set for most installations. Python can find its standard library. So, the only reason to use PYTHONPATH variables is to maintain directories of custom Python libraries that are not installed in the site packages directory (the global default location). In simple terms, it is used by user-defined modules to set the path so that they can be directly imported into a Python program. It also handles the default search path for modules in Python. PYTHONPATH variable includes various directories as a string to be added to the sys.path directory list. So, with PYTHONPATH, users can import modules that have not been made installable yet.
Here is an example. The module sample.py has the following content:
def function():
print("Function is running")
The script sample_script.py has the following content:
import sample
.function()
Here is the output:
$ python3 scripts/sample_script.py
Traceback (most recent call last):
File "scripts/sample_script.py", line 1, in <module>
import sample
ModuleNotFoundError: No module named 'sample'
We get such an output because the PYTHONPATH has not been set yet. In layman's terms, the Python interpreter cannot find the location of the sample.py file. In the next section, we will examine how you can set the PYTHONPATH environment variable on different operating systems.
Setting Python Environment Variable PYTHONPATH on Mac
To set the python environment variable PYTHONPATH on Mac, follow the given steps:
Step 1: Open the Terminal.
Step 2: In your text editor, open the ~/.bash_profile file. For example: atom ~/.bash_profile;
Step 3: To this file, add the following line at the bottom:
export PYTHONPATH="/Users/my_user/code"
Step 4: Save this text editor file.
Step 5: Close the terminal.
Step 6: Restart the terminal. You can now read the new settings.
Type: echo $PYTHONPATH
It would show something like /Users/my_user/code. That is it. PYTHONPATH is set.
Setting the Python Environment Variable PYTHONPATH on Linux
To set the python environment variable PYTHONPATH on Linux, follow the given steps:
Step 1: Open up the terminal
Step 2: In your text editor, open the ~/.bashrc file. For example: atom ~/.bashrc;
Step 3: In this text editor, add this line at the end:
export PYTHONPATH=/home/my_user/code
Step 4: Save this text editor file.
Step 5: Close the terminal application
Step 6: Restart the terminal application. You can read in the new settings. Type:
echo $PYTHONPATH
It will show something like /home/my_user/code:
You are done setting the PYTHONPATH on your Linux system.
Setting the Python Environment Variable PYTHONPATH on Windows
Here is how to set PYTHONPATH on a windows machine:
Step 1: Open My Computer or This PC and right-click on it. Then click on properties.
Step 2: When the properties window pops up, click on the Advance System Settings.
Step 3: Click on the environment variable button that appears in the new popped-up window. Here is how it looks:
Step 4: In the dialog box- new Environment Variable, click on New.
Step 5: In the variable dialog box, add the variable's name as PYTHONPATH. Add the location that you want Python to check every time as a value to the module directory.
Step 6: Open the command prompt, execute the python file using the given command:
python my_script.py
(Here, my_script is the name of the python file).
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer
Conclusion
We have reached the end of this blog. We saw how you could set the PYTHONPATH environment variable in Python on all three standard operating systems- Linux, Windows, and macOS. After setting up the PYTHON PATH, you can import user-defined modules. And if you wish to master Python, a certification in Python is all you need! However, if you wish to master full-stack development and build a successful career in this field, our PGP in Full Stack Development, in collaboration with Caltech CTME must be your next step.
And on the other hand, if you have any questions in terms of python path, do write to us in the comment section below, and our experts will get back to you!