Python as a language comes with a lot of flexibility and scalability. The ability to group files into a package and then group packages into a library makes Python very powerful. But controlling the accessibility of Python files is also an important task. Python has environment variables like pythonpath
for this very purpose.
In this article, we will dive deep into Python’s environment variables and thoroughly understand the Pythonpath variable. Later, we’ll compare it to path variables in other languages. So let’s get started with it.
What is PYTHONPATH?
The pythonpath
the variable is an environment variable that stores paths for all the directories where the interpreter is supposed to search for the modules or packages requested to be imported.
Related: Learn how to import other Python files.
Pythonpath basically operates based on the sys.path
the sys
library. sys.path
is a list of paths where the interpreter is supposed to search for the modules or packages which has been requested to be imported. The pythonpath appends all the directories specified in it to the sys.path
list provided by the sys
library.
Directories present in pythonpath
When you download Python in your system, a default directory location is added to the pythonpath where all the modules that are part of the standard library and all the libraries installed globally in your system are stored.
The second path that is added to the pythonpath is the location of the directory where the file you’ve created is present. This allows the interpreter to reach for the modules and packages present at the same relative position as the Python file you’re programming in.
Apart from this, you can append other directories where you want your interpreter to search for the modules and packages to sys.path
list. You can do this by the following block of code.
import sys
sys.path.append("path/to/directory")
Comparison with the PATH environment variable in other languages
The path variables in other languages serve the same purpose as the pythonpath
variable. It is used to specify where to search for modules and packages imported. In C/C++, the getenv() method is used to access the path variable. The path variable can be the same for many languages. It is even the same for Python. But pythonpath
provides more versatility.
Importance of setting the PYTHONPATH
Importing other modules and packages so you don’t have to write the same code repeatedly satisfies an OOP language’s DRY(Don’t repeat yourself) property. Setting up the pythonpath variable helps in organizing files and easily accessing a file in need. It helps in isolating the program from different versions of the same dependencies in a separate virtual environment.
Ways to Get the PYTHONPATH
in Shell
Using the echo
command
The echo
command is the print statement in shell scripting. It’s present in both – the terminal and command prompt. Most of the os’ have the echo
command. You can use it to output text or value of variables in the terminal itself. We’re going to make it output the pythonpath
. Let’s see how we can use the echo
command to get the value of the pythonpath
variable.
echo $PYTHONPATH

Note: If you try to print pythonpath
right now, it will output nothing in most systems as pythonpath
isn’t set by default in most systems. We’ll learn how to set pythonpath
further in the article.
Using the python
command
Let’s try to get the pythonpath
using the python
command in the shell.
python -c "import os;pythonpath = os.getenv('PYTHONPATH','');print(os.getenv('PYTHONPATH'))"

Using the -c
flag, we executed a Python script directly in the shell. We checked for pythonpath using the os module.
Related: Learn more on How to execute Python code in shell?
Using the os
module in Python
The os
module can be used to work with different operation system-related operations. The os
module can be used to access the pythonpath
variable.
import os
pythonpath = os.getenv('PYTHONPATH','')
pythonpath+='/Users/kundansingh/opt/anaconda3/lib/python39.zip'
os.environ['PYTHONPATH']=pythonpath
print(os.getenv('PYTHONPATH'))

First, you’ll access the pythonpath
using the os.getenv
function. Then you’ll add the paths to it which you want to add. After that, you’ll set the pythonpath
in the os.environ
dictionary.
Note: You can’t modify the pythonpath
using the os module permanently. The only way to get the pythonpath environment variable and to modify it is through shell/bash commands like echo and export.
Setting the PYTHONPATH
In most of the systems, the pythonpath
is not set by default. You will have to set it manually using the shell commands. There are mainly 2 ways using which you can set the pythonpath
variable. Let’s explore both of them.
Using the export command
The export
command can access the environment variables and set or update their value. It’s really simple to use. It can be used to set the pythonpath
variable.
export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9:$PYTHONPATH"

Note: Using the export
command sets the pythonpath
only for the particular shell session. It is persistent but is still temporary. Therefore we’re going to use bash to actually set the pythonpath variable, which we can use whenever we want.
Using the .bashrc file
Change your current directory to the home directory, and then open a .bashrc file by executing the nano .bachrc command. After that, write the following line of code in it.
export PYTHONPATH="/Users/kundansingh/opt/anaconda3/lib/python39.zip"
Now set the source as the .bashrc
, and your pythonpath
is set.
Common Issues and Solutions
- The most common issue that arises while working with
pythonpath
is incorrect syntax. A key place where most programmers make mistakes is the paths are separated with":"
in macOS or Linux and with";"
in Windows. - Another thing that may cause an issue is the path type. Using the Absolute path is preferable while working the
pythonpath
the relative path is program specific and may cause issues in other programs. - Make sure you’re using the new shell session every time you work on a new project to avoid using the wrong dependencies.
- Make sure you’re changing the
pythonpath
in the correct environment.
Conclusion
That’s all you need to know about the pythonpath
variable. It’s not that big of a thing and is very simple to understand. But it provides a lot of flexibility to the programmers. Make sure you keep experimenting with it and see what you can achieve using it. Keep exploring new things cause that’s what makes a programmer a good programmer.
References
Official Python Documentation.
Stack Overflow answer for the same question.