You don't set PYTHONPATH, you add entries to sys.path. It's a list of directories that should be searched for Python packages, so you can just append your directories to that list.

import sys

sys.path.append('/path/to/whatever')

In fact, sys.path is initialized by splitting the value of PYTHONPATH on the path separator character (: on Linux-like systems, ; on Windows).

You can also add directories using site.addsitedir, and that method will also take into account .pth files existing within the directories you pass. (That would not be the case with directories you specify in PYTHONPATH.)

Answer from David Z on Stack Overflow
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › using_pythonpath.html
Using PYTHONPATH — Functional MRI methods
$ # Set PYTHONPATH to path to the working directory + /code $ # This is for the "bash" shell on Unix / git bash on Windows $ export PYTHONPATH="$PWD/code" $ # Now the script can find "a_module" $ python3 scripts/a_script.py Running useful function
Discussions

Setting PYTHONPATH
Aim:- I would want to set python path programmatic in my project. So that, all other directories files can be imported without any issues. Problem statement:- I used to add all the folders & sub-folders in environment variables under PYTHONPATH but this has two constrains. More on discuss.python.org
🌐 discuss.python.org
1
0
December 14, 2019
Need to set PYTHONPATH in order to make modules get detected.
Those paths come from the "site" module. You can see how it's configured with "python3 -m site". You built python, but almost certainly needed to pass certain arguments to make to get the right results. If you look at your distro, you can probably find the script they use to compile the official package. More on reddit.com
🌐 r/Python
13
0
November 9, 2024
Setting the correct PYTHONPATH
Can you share your .bashrc? Does it something look like this? export PATH="/home/anaconda/bin:$PATH" More on reddit.com
🌐 r/Python
5
1
June 25, 2015
python - What exactly should be set in PYTHONPATH? - Stack Overflow
I'm going through and writing a setup doc for other developers at work for a python project and I've been reading up on the PYTHONPATH environment variable. I'm looking at my current development sy... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

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.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding the python path environment variable in python
Understanding the Python Path Environment Variable in Python [Updated]
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:set Pythonpath=C:\python On Mac or Linux, you can use the export command to create a Pythonpath variable:export Pythonpath =/usr/local/lib/python
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding the python path environment variable in python
Understanding the Python Path Environment Variable in Python [Updated]
🌐
Python.org
discuss.python.org › python help
Usage of PYTHONPATH - Python Help - Discussions on Python.org
August 9, 2022 - The results of my research are ... values: C:\Program Files\Python\DLLs C:\Program Files\Python\Lib C:\Program Files\Python C:\Program Files\Python\Lib\site-packages C:\Users\…\AppData\Roaming\Python\Python310\site-packages I created ...
🌐
Redreamality
redreamality.com › blog › pythonpathvs-code
How to Set PYTHONPATH on Windows, Linux, macOS and VS Code
August 21, 2025 - PYTHONPATH is an environment variable that allows users to add extra directories to the list of paths that the Python interpreter searches for modules and packages. This is particularly useful during development when you need to import custom modules that are not located in the standard library path or in the installed package directories. This article will detail various methods for setting PYTHONPATH on Windows, macOS, and Linux systems.
🌐
Net Informations
net-informations.com › python › intro › path.htm
How to set python path
csh shell: type setenv PATH "$PATH:/usr/local/bin/python" and press Enter. bash shell (Linux): type export ATH="$PATH:/usr/local/bin/python" and press Enter. sh or ksh shell: type PATH="$PATH:/usr/local/bin/python" and press Enter. /usr/local/bin/python is the default path of the Python directory.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-set-python-environment-variable-pythonpath-on-linux
How to set Python environment variable PYTHONPATH on Linux?
March 24, 2026 - Open a terminal and use the export command to set PYTHONPATH ? export PYTHONPATH=/home/user/myproject:$PYTHONPATH · This command adds /home/user/myproject to PYTHONPATH while preserving any existing paths. The colon (:) separates multiple paths on Linux. ... This displays the current PYTHONPATH value, showing all directories Python will search for modules.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › pythonpath-environment-variable-in-python
PYTHONPATH Environment Variable in Python - GeeksforGeeks
January 9, 2026 - Step 2: After the properties window pop up click on Advanced 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 ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding the python path environment variable in python
Understanding the Python Path Environment Variable in Python [Updated]
September 14, 2025 - Python path is an environment variable used to maintain directories of custom Python libraries. Learn how to set a python path variable on Windows and Mac now!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Real Python
realpython.com › add-python-to-path
How to Add Python to PATH – Real Python
July 23, 2026 - >>> PATH = "/home/realpython/apps:/bin" ... a string separated by colons, prepending a value involves creating a string with the new path, a colon, then the old path....
🌐
Python.org
discuss.python.org › python help
Setting PYTHONPATH - Python Help - Discussions on Python.org
December 14, 2019 - Aim:- I would want to set python path programmatic in my project. So that, all other directories files can be imported without any issues. Problem statement:- I used to add all the folders & sub-folders in environment variables under PYTHONPATH but this has two constrains.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-set-your-python-path-on-windows
How to set your python path on Windows?
March 24, 2026 - The PYTHONPATH environment variable tells Python where to look for modules and packages beyond the standard library. While modifying PYTHONPATH is rarely needed, here's how to set it on Windows when necessary.
🌐
Reddit
reddit.com › r/python › setting the correct pythonpath
r/Python on Reddit: Setting the correct PYTHONPATH
June 25, 2015 -

I'm trying to set my PYTHONPATH to use the anaconda package I just installed, but whenever I try to import a module it tries to load it from the old path. I see my new path when I print sys.path, but it's behind the old path in the list. I installed anaconda in my home directory on a computer cluster, and so I can't uninstall the original path. Does anyone know why the current path set in the .bashrc file is not the priority for sys.path, and how I could fix this so only the path to anaconda works for loading modules? Thanks for any help or advice.

edit: I have contacted the support for the computer cluster, but haven't heard back yet. I'm looking here to try to get any advice on how to fix this as quickly as possible.

Top answer
1 of 4
84

You don't have to set either of them. PYTHONPATH can be set to point to additional directories with private libraries in them. PYTHONHOME sets the location of default libraries.

Documentation:

PYTHONHOME
Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.

When PYTHONHOME is set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, set PYTHONHOME to prefix:exec_prefix.

PYTHONPATH
Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.

2 of 4
31

For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.

The only reason to set PYTHONPATH is to maintain directories of custom Python libraries that you do not want to install in the global default location (i.e., the site-packages directory).

Make sure to read: http://docs.python.org/using/cmdline.html#environment-variables

🌐
Medium
bryantson.medium.com › use-of-pythonpath-for-python-setup-fe36eb0a8cae
Use of “PYTHONPATH” for Python setup | by Bryant Jimin Son | Medium
January 26, 2026 - Use of “PYTHONPATH” for Python setup PYTHONPATH PYTHONPATH is an environment variable that affects Python’s module and package search path. It allows you to configure additional directories …
🌐
TutorialsPoint
tutorialspoint.com › article › What-is-PYTHONPATH-environment-variable-in-Python
What is PYTHONPATH environment variable in Python?
August 26, 2023 - When Python imports a module, it ...ib/python3.9/site-packages /usr/local/lib/python3.9/dist-packages · You can set PYTHONPATH before running Python scripts....
Top answer
1 of 2
95

This will add a path to your Python process / instance (i.e. the running executable). The path will not be modified for any other Python processes. Another running Python program will not have its path modified, and if you exit your program and run again the path will not include what you added before. What are you are doing is generally correct.

set.py:

import sys
sys.path.append("/tmp/TEST")

loop.py

import sys
import time
while True:
  print sys.path
  time.sleep(1)

run: python loop.py &

This will run loop.py, connected to your STDOUT, and it will continue to run in the background. You can then run python set.py. Each has a different set of environment variables. Observe that the output from loop.py does not change because set.py does not change loop.py's environment.

A note on importing

Python imports are dynamic, like the rest of the language. There is no static linking going on. The import is an executable line, just like sys.path.append....

2 of 2
9

As also noted in the docs here.

Go to Python X.Y/Lib (Windows) or /usr/lib/pythonX.Y (Linux) and add these lines to the site.py there,

import sys
sys.path.append("yourpathstring")

This changes your sys.path so that on every load, it will have that value in it..

As stated here about site.py,

This module is automatically imported during initialization. Importing this module will append site-specific paths to the module search path and add a few builtins.

For other possible methods of adding some path to sys.path see these docs

🌐
Upgrad
upgrad.com › home › blog › data science › python path: what it is and how to set it up correctly
Python Path: What It Is and How to Set It Up Correctly
July 24, 2026 - You can manually add the Python folder to your system's PATH variable through environment variable settings on Windows, or by editing your shell configuration file on Mac and Linux.