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.
What Is the PYTHONPATH Environment Variable in Python?
Pythonpath is a special environment variable that provides guidance to the Python interpreter about where to find various libraries and applications. It is similar to the PATH environment variable in other languages, such as C and Java, but has additional directories for Python modules.
Pythonpath environment variable in Python allows you to include paths to other Python files in your scripts and Python’s import mechanism to determine where to search for modules and files that use it. This can be useful if you want to access different pieces of functionality from within your script without having to type out the full path every time.
Pythonpath can be either a single directory or a list of directories separated by colons (:).
- If Pythonpath is set to a single directory, Python will search for modules and files in that directory only.
- Or if Pythonpath is set to a list of directories, Python will search for modules and files in each of those directories.
When you launch the Python interpreter, it looks for Pythonpath inside your current working directory. If it doesn’t find an entry there, it will search for Pythonpath in each parent folder until it finds one or creates one, if necessary. And it is typically set by the Python installation program, but can be overridden by the user.
Understanding the Pythonpath Environment Variable
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:
print(“Function is running”)
The script sample_script.py has the following content:
Here is the output:
$ python3 scripts/sample_script.py
Traceback (most recent call last):
File “scripts/sample_script.py”, line 1, in <module>
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:
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:
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:
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:
(Here, my_script is the name of the python file).
1. How do I add an environment variable in Pythonpath?
Following a few steps will allow you to add an environment variable:
- By setting the Pythonpath environment variable.
- By adding manually to the path in the file.
- By using the os.environ module method.
- Then you can also use the os.environ[“key”] = “value” syntax to add a single environment variable.
2. How do I create a Pythonpath variable?
To create a Pythonpath variable, you will need to first open the Command Prompt.
Once the Command Prompt is open, you will need to type in the following command: setx /M Pythonpath “C:\Python27\Lib”.
And you should know that creating a Pythonpath environment variable in Python will be changing depending on your operating system and whether you want the variable to be permanent.
- On Windows, you can use the set command to create a Pythonpath variable:
- On Mac or Linux, you can use the export command to create a Pythonpath variable:
export Pythonpath =/usr/local/lib/python
3. What is Pythonpath used for?
Pythonpath is an environment variable that is used to specify the location of Python libraries. It is typically used by developers to ensure that their code can find the required Python libraries when it is run.
4. Where is Pythonpath located?
Pythonpath is typically located in the same directory as the Python interpreter.
5. Is SYS path the same as Pythonpath?
- The SYS path is a list of directories that Python interprets to search for when it starts up.
- The Pythonpath is a list of directories that the Python interpreter will search for when it tries to resolve a module name.
So, both look the same but are quite different.
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
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!