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.

Discussions

Where is python installed on Linux
Check /usr/bin/, most executables that come with the install should be there. More on reddit.com
🌐 r/learnpython
8
2
February 10, 2021
Sys.path does not have current directory by default
Hi, I am trying to do a debug build of Python-3.11.1 on NonStop systems(custom OS and somewhat similar to linux), I see that whenever I invoke the interpreter, sys.path does not have the current directory by default. Th… More on discuss.python.org
🌐 discuss.python.org
15
0
March 29, 2023
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί pathlib.html
pathlib β€” Object-oriented filesystem paths
February 23, 2026 - Source code: Lib/pathlib/ This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which pro...
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί os.path.html
os.path β€” Common pathname manipulations
Since different operating systems ... the standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths....
🌐
Real Python
realpython.com β€Ί add-python-to-path
How to Add Python to PATH – Real Python
January 30, 2023 - In this tutorial, you’ve learned how to add Python, or any other program, to your PATH environment variable on Windows, Linux, and macOS. You also learned a bit more about what PATH is and why its internal order is vital to consider.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί where is python installed on linux
r/learnpython on Reddit: Where is python installed on Linux
February 10, 2021 -

Hi, I have anaconda installed, so typing:

which python

just returns the anaconda path, however, I need to unearth the standard install location as a script is annoyingly asking for python2.7. Google unfortunately only tells you how to install it, and I know that, I've just forgotten where the binary is usually located on linux and google won't tell me that no matter what I type in.

UPDATE: Thanks guys, I found it looking in /usr/bin/ and /usr/local/bin/ and to my delight, it's not there, Just upgraded to Linux Mint 20 and there is python3.8. lovely.

Find elsewhere
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.
    >>> 
    
🌐
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
🌐
Quora
quora.com β€Ί How-do-I-find-a-Python-path-in-Linux
How to find a Python path in Linux - Quora
In the csh shell, type the following sentence: PATH β€œ$PATH:/usr/local/bin/python” and press Enter. If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin...
🌐
Linux Mint Forums
forums.linuxmint.com β€Ί board index β€Ί main edition support β€Ί beginner questions
Python PATH - Linux Mint Forums
PATH is a shell construct (a standard shell construct: one you can expect any specific shell such as bash, dash, ash, ..., to support). PYTHONPATH is much less generic, since Python-interpreter specific, and serves a moreover very different purpose: it only provides the Python interpreter with a list of locations to search for imported modules, not the system with one to search for (Python) script/applications itself.
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-set-python-environment-variable-pythonpath-on-linux
How to set Python environment variable PYTHONPATH on Linux?
May 2, 2023 - This command sets the PYTHONPATH environment variable to /home/user/myproject and also includes the previous value of PYTHONPATH in case it was already set. Note that the path should be separated by a colon (:) on Linux.
🌐
Visual Studio Code
code.visualstudio.com β€Ί docs β€Ί python β€Ί environments
Python environments in VS Code
November 3, 2021 - Workspace β”œβ”€β”€ Python Project: backend/ β”‚ └── Environment: .venv (Python 3.12) β”‚ └── Manager: venv β”‚ β”œβ”€β”€ Python Project: frontend-utils/ β”‚ └── Environment: .venv (Python 3.10) β”‚ └── Manager: venv β”‚ └── Python Project: ml-pipeline/ └── Environment: ml-env (Python 3.11) └── Manager: conda ... Test Explorer: each project gets its own test tree with its own interpreter (see Multi-Project Testing) ... Pylance and Jupyter currently use a single interpreter per workspace, not per-project environments. See Known Limitations. ... When you add a project, its folder is automatically added to the environment search path.
🌐
Python Land
python.land β€Ί home β€Ί install python: detailed instructions for window, mac, and linux
Install Python: Detailed Instructions for Window, Mac, and Linux β€’ Python Land Tutorial
October 1, 2024 - It’s easy and works like the installation of any other MacOS software program. The downside to this approach is that you won’t get automatic updates. Just like with Windows, you should ensure that Python is added to your system’s PATH. There are several ways to install Python on Linux, that is if you need to install it at all!
🌐
Python Doctor
python.doctor β€Ί page-python-path-pythonpath
Python path
>>> import sys >>> 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/python2.7/dist-packages/ubuntu-sso-client']
🌐
Edureka
edureka.co β€Ί blog β€Ί add-python-to-path
Add Python To Path | How To Add A Python Path | Edureka
December 5, 2024 - In the csh shell, type the following sentence: PATH β€œ$PATH:/usr/local/bin/python” and press Enter. If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin...
🌐
AskPython
askpython.com β€Ί home β€Ί determining the install path of python from the command line
Determining the Install Path of Python from the Command Line - AskPython
June 30, 2023 - You must have seen in tutorials regarding Python installation, it is always advised to add Python to PATH. What does this mean? It means that you are making Python commands accessible from your command prompt.
🌐
Quora
quora.com β€Ί How-can-I-find-where-Python-is-installed-on-Linux
How to find where Python is installed on Linux - Quora
1) Use which/type/command to locate the executable ... These show the path of the executable that your shell will run (e.g., /usr/bin/python3, /usr/local/bin/python3, ~/.local/bin/python).