1. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:

    # make python look in the foo subdirectory of your home directory for   
    # modules and packages 
    export PYTHONPATH=${PYTHONPATH}:${HOME}/foo 
    

    Here I use the sh syntax. For other shells (e.g. csh,tcsh), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).

  2. Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.

  3. The folder where your modules live is dependent on PYTHONPATH and where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the command ls -- where does ls live? /usr/bin? /bin? 99% of the time, you don't need to care -- Just use ls and be happy that it lives somewhere on your PATH so the shell can find it.

  4. I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.

  5. Configure PYTHONPATH to include the directory where your module resides and python will be able to find your module.

Answer from mgilson on Stack Overflow
Top answer
1 of 3
73
  1. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:

    # make python look in the foo subdirectory of your home directory for   
    # modules and packages 
    export PYTHONPATH=${PYTHONPATH}:${HOME}/foo 
    

    Here I use the sh syntax. For other shells (e.g. csh,tcsh), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).

  2. Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.

  3. The folder where your modules live is dependent on PYTHONPATH and where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the command ls -- where does ls live? /usr/bin? /bin? 99% of the time, you don't need to care -- Just use ls and be happy that it lives somewhere on your PATH so the shell can find it.

  4. I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.

  5. Configure PYTHONPATH to include the directory where your module resides and python will be able to find your module.

2 of 3
46
  1. PYTHONPATH is an environment variable
  2. Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
  3. /usr/lib/python2.7 on Ubuntu
  4. you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
  5. if you use pip or setuptools, then you don't need to set PYTHONPATH explicitly

If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.

🌐
AskPython
askpython.com › python › built-in-methods › pythonpath-in-shell
How to Get the Pythonpath in Shell? - AskPython
1 month ago - Python can read the environment inherited from its launching shell with os.getenv. The following command assigns an empty-string fallback to pythonpath but prints a separate lookup without that fallback, so an unset variable prints None. python3 -c "import os;pythonpath = os.getenv('PYTHONPATH','');print(os.getenv('PYTHONPATH'))"
Discussions

ubuntu - How to set up Python path? - Unix & Linux Stack Exchange
I have uninstall anaconda2. but now when I run Python command in terminal it says "bash: /home/user/anaconda2/python: No such file or directory" now how can I set to Python when I have python 2.7 i... More on unix.stackexchange.com
🌐 unix.stackexchange.com
December 31, 2015
python - how to set different PYTHONPATH variables for python3 and python2 respectively - Stack Overflow
I want to add a specific library path only to python2. After adding export PYTHONPATH="/path/to/lib/" to my .bashrc, however, executing python3 gets the error: Your PYTHONPATH points to a site-pack... More on stackoverflow.com
🌐 stackoverflow.com
How do I find out my PYTHONPATH using Python? - Stack Overflow
PYTHONPATH is an environment variable that effects this list. By any reasonable definition sys.path is your "python path". ... Don't forget about python -m site. ... "python -m site --user-site ... If it is called without arguments, it will print the contents of sys.path on the standard output..." ... Caveat: If you have multiple versions of Python installed you should use a corresponding command python2 or python3. ... Same. Linux ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to get the PYTHONPATH in shell? - Stack Overflow
debian@debian:~$ echo $PYTHONPATH /home/qiime/lib/: debian@debian:~$ python Python 2.7.3 (default, Jan 2 2013, 16:53:07) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "li... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I add PYTHONPATH inside a Python script?
Changing os.environ["PYTHONPATH"] changes the process environment and can affect child processes started afterwards. For an import in the running interpreter, add the intended directory to sys.path before importing, or install the package into that interpreter’s environment.
🌐
askpython.com
askpython.com › python › built-in-methods › pythonpath-in-shell
How to Get the Pythonpath in Shell? - AskPython
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]
🌐
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 - Python search paths: /usr/lib/python3.x/site-packages /home/user/myproject ... Use export PYTHONPATH to add custom module paths on Linux. Add the export command to ~/.bashrc for permanent configuration.
🌐
Real Python
realpython.com › add-python-to-path
How to Add Python to PATH – Real Python
July 23, 2026 - Since Python typically comes pre-installed on UNIX-based systems, the most common problem on Linux and macOS is for the wrong python to run, rather than not finding any python. That said, in this section, you’ll be troubleshooting not being able to run python at all. Note: Depending on your particular system, you may have a python program for Python 2, and a python3 for Python 3.
🌐
LinuxVox
linuxvox.com › blog › python-path-linux
Mastering Python Paths on Linux — linuxvox.com
Warning: PYTHONPATH affects all installed Python versions and environments on your system. Be cautious about setting it in your shell profile or global environment variables, as it can cause unexpected behavior across different projects. In most cases, using virtual environments is a better approach. The site-packages directory is where Python installs third-party packages by default. On Linux, this is typically located at /usr/lib/python3.x/site-packages/ or /usr/local/lib/python3.x/dist-packages/ (depending on your distribution).
🌐
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
Find elsewhere
🌐
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
Top answer
1 of 4
5

I'm assuming that when you installed anaconda 2, you manually set the PYTHONPATH environment variable, by putting something like

PYTHONPATH=/home/user/anaconda2/python
export PYTHONPATH

in your .bash_profile or .bash_rc.

But since you deleted the /home/user/anacanda2/ directory, that path no longer exists.

Thus you want to change PYTHONPATH to point to the executable in /usr/lib, by changing the above to

PYTHONPATH=/usr/lib/my_python_distribution
export PYTHON
2 of 4
3
  1. execute the command: echo $PATH

    root1@master:/usr/lib/python2.7$ echo $PATH
    /home/root1/anaconda3/bin:/home/root1/NAI/Execution/HDE/x86_64.linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/root1/java/jdk1.8.0_74/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/root1/NAI/hadoop-2.7.3/bin
    
  2. Remove your anaconda3 from your path variable that is

    /home/root1/anaconda3/bin:
    
  3. Again set PATH variable with remaining information like below

    export PATH=/home/root1/NAI/Execution/HDE/x86_64.linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/root1/java/jdk1.8.0_74/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/root1/NAI/hadoop-2.7.3/bin
    
  4. Execute python command and should redirect to your python interpreter

    root1@master:/usr/lib/python2.7$ python
    Python 2.7.14 (default, Sep 18 2017, 00:00:00) 
    [GCC 4.8.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
🌐
Edureka
edureka.co › blog › add-python-to-path
Add Python To Path | How To Add A Python Path | Edureka
December 5, 2024 - In some instances, PYTHONPATH is already preset in the Python directory. PYTHONSTARTUP: The use of this file contains the path of an initialization file which contains the Python source code. This function is executed every time you start the interpreter in Python. In Unix or Linux, this is named as pythonrc.py and using this command loads the utilities that modify PYTHONPATH.
Top answer
1 of 4
31

PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.

This could sound more tricky than it is, so let me show you how it works.

Let us assume your "package" has a single file named wow.py and you keep it in /home/user/mylib/wow.py.

Create the file /home/user/mylib/setup.py with the following content:

from setuptools import setup

setup(name="WowPackage",
      packages=["."],
)

That's it, now you can "properly install" your package into the Python distribution of your choice without the need to bother about PYTHONPATH. As far as "proper installation" is concerned, you have at least three options:

  • "Really proper". Will copy your code to your python site-packages directory:

    $ python setup.py install
    
  • "Development". Will only add a link from the python site-packages to /home/user/mylib. This means that changes to code in your directory will have effect.

    $ python setup.py develop
    
  • "User". If you do not want to write to the system directories, you can install the package (either "properly" or "in development mode") to /home/user/.local directory, where Python will also find them on its own. For that, just add --user to the command.

    $ python setup.py install --user
    $ python setup.py develop --user
    

To remove a package installed in development mode, do

$ python setup.py develop -u

or

$ python setup.py develop -u --user

To remove a package installed "properly", do

 $ pip uninstall WowPackage

If your package is more interesting than a single file (e.g. you have subdirectories and such), just list those in the packages parameter of the setup function (you will need to list everything recursively, hence you'll use a helper function for larger libraries). Once you get a hang of it, make sure to read a more detailed manual as well.

In the end, go and contribute your package to PyPI -- it is as simple as calling python setup.py sdist register upload (you'll need a PyPI username, though).

2 of 4
25

You can create a configuration file mymodule.pth under lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh), then add one line containing the directory to add to python path.

From docs.python2 and docs.python3:

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.

Top answer
1 of 3
9

You're absolutely right, they use different PYTHONPATHs.

You can think of Python 2.x and Python 3.x as completely different programming environments. And yes, they store their packages in different locations.

To get numpy working, you can type:

sudo apt-get install python3-numpy

If you want to find out where exactly a package is kept, you can look at the module objects __path__ attribute:

>>> import numpy
>>> numpy.__path__
['/usr/local/lib/python3.5/site-packages/numpy']

You can also install python3-pip and then run pip3 install whatever to install packages for Python 3 with Pip, for packages that aren't available in Ubuntu as python3-whatever.

In case you're confused about the difference between distutils, setuptools, easy_install, pip and the rest, use pip. That's the cool one. :)

2 of 3
5

No, they use the same PATH. However, this problem isn't with the PATH.

Python 2 and Python 3 are sufficiently different that packages have to be written separately for both of them. You cannot use a package written for one with the other.

In Ubuntu, these modules are stored in different locations and are packaged separately - python-numpy for Python 2, python3-numpy for Python 3. If you want numpy with Python 3, install python3-numpy.

$ python3 -c 'import sys; print (sys.path)'
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/home/muru/.local/lib/python3.4/site-packages', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
$ python2 -c 'import sys; print (sys.path)'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
🌐
Redreamality
redreamality.com › blog › pythonpathvs-code
How to Set PYTHONPATH on Windows, Linux, macOS and VS Code
August 21, 2025 - Additionally, you can check sys.path in the Python interpreter, which is a list of all module search paths. The directories specified in PYTHONPATH should appear in this list. ... Separator: Windows uses a semicolon (;), while macOS and Linux use a colon (:).
🌐
YouTube
youtube.com › watch
PYTHONPATH Explained, How to Use PYTHONPATH | How to Python 1 | Python Tutorials - YouTube
In this video we’ll be go over how to set up your PYTHONPATH in a linux terminal and windows command prompt to be able to use GitHub repositories you’ve clon...
Published: October 7, 2021
Views: 8K
🌐
The Tech Deck
thetechdeck.hashnode.dev › how-to-add-python-to-your-path-on-linux
How to Add Python to Your PATH on Linux
December 11, 2023 - When you install Python on your Linux system, the main Python executable file (python3) goes to a directory like /usr/bin.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-set-your-python-path-on-linux
How to set your python path on Linux?
March 24, 2026 - To set the PYTHONPATH on Linux to point Python to look in other directories for the module and package imports, you can use the export command.
🌐
Quora
quora.com › How-do-I-find-a-Python-path-in-Linux
How to find a Python path in Linux - Quora
Answer (1 of 5): This works with every executable - use the which command [code]$> which python3.9 [/code]If you are able to use the command ‘python3.9’ at the command line then the ‘which’ command above will tell you the path to that command. If you need to find the path from within ...