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.

๐ŸŒ
Medium
medium.com โ€บ @yulin_li โ€บ the-difference-between-different-ways-of-specifying-shared-library-paths-for-python-and-system-101fd7cda667
Different ways of specifying shared library paths for Python and system level on Ubuntu | by Lynn | Medium
July 12, 2022 - If the pip you are using corresponds to your manually compiled python from source, then its own packages and the packages installed by it would both be located in /usr/local/lib/python3.6/site-packages. There are two ways for system to look for the shared library, especially for C++ codes in my case. The first one is by editing files in /etc/ld.so.conf.d. The second one is by using the LD_LIBRARY_PATH environment variable. Under Linux, the search and loading of shared libraries is implemented by /lib/ld.so.
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
Are the paths to the python standard library and to the dist-packages different for different linux distributions(Debian, Redhat, Slackware, ...)? - Stack Overflow
My question assumes you are using the same python versions on those different linux distributions. I also exclude virtual environments from my question. I use the Debian based distribution Ubuntu. There the path to the python standard library (the modules/packages written in python) is ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Library Path - Stack Overflow
In ruby the library path is provided in $:, in perl it's in @INC - how do you get the list of paths that Python searches for modules when you do an import? More on stackoverflow.com
๐ŸŒ stackoverflow.com
setuptools - How to find out the Python library installation path? - Stack Overflow
Note: This works after the package ... the sys.path. ... Save this answer. ... Show activity on this post. I don't know of a good way to do this in Python (other than crawling the directories from a given root to look for a file using os.walk). If you do not get any better answers for how to do this in Python you could execute a command on your OS from python to execute a standard Linux/Unix find command on the library you are looking ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ programming โ€บ python
How to get Python to use a custom library path - Raspberry Pi Forums
July 2, 2024 - Python 3.7.3 (/usr/bin/python3) >>> %Run Python_Path.py <module 'easygopigo3' from '/home/pi/.local/lib/python3.7/site-packages/gopigo3-1.3.2.1-py3.7.egg/easygopigo3.py'> >>> Note that this is still the default system path to the library .egg file instead of my custom installation of these libraries.
๐ŸŒ
Python
docs.python.org โ€บ 3.3 โ€บ install โ€บ index.html
Installing Python Modules โ€” Python 3.3.7 documentation
September 19, 2017 - >>> import sys >>> sys.path ['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2', '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload', '/usr/local/lib/python2.3/site-packages'] >>>
๐ŸŒ
Linuxtopia
linuxtopia.org โ€บ online_books โ€บ programming_books โ€บ python_programming โ€บ python_ch28s04.html
Python - Finding Modules: The Path
For modules to be available for use, the Python interpreter must be able to locate the module file. Python has a set of directories in which it looks for module files. This set of directories is called the search path, and is analogous to the PATH environment variable used by an operating system ...
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.
    >>> 
    
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-to-set-python-environment-variable-PYTHONPATH-on-Linux
How to set Python environment variable PYTHONPATH on Linux?
May 2, 2023 - Use export PYTHONPATH to add custom module paths on Linux. Add the export command to ~/.bashrc for permanent configuration.
Find elsewhere
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ pathlib.html
pathlib โ€” Object-oriented filesystem paths
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...
๐ŸŒ
Leemendelowitz
leemendelowitz.github.io โ€บ blog โ€บ how-does-python-find-packages.html
How does python find packages? // Lee On Coding // My blog about coding and stuff.
>>> import sys >>> print '\n'.join(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
๐ŸŒ
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
Top answer
1 of 3
171

How python finds its modules

Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).

By default, Python looks for its modules and packages in $PYTHONPATH.

To find out what is included in $PYTHONPATH, run the following code in python (3):

import sys
print(sys.path)

How to add a directory

Occasionally

From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:

import sys
sys.path.insert(0, "/path/to/your/package_or_module")

For example:

if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:

import sys
sys.path.insert(0, "/home/myname/pythonfiles")

And I can simply import the file module_1.py by:

import module_1

When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py

For example:

To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `

from <packagename> import module_2

Adding directories to $PYTHONPATH permanently:

Add the following line to your ~/.profile file.

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add

Subdirectories

From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:

import packagename.subfolder.module_3

Given the fact that all subfolders in the package include their own __init__.py file.

When a module is in the same directory as the script or application

There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.

Example:

If I have a folder, containing script.py and module.py, I can simply import the module by:

import module
2 of 3
7

The correct way to use relative import is:

from ..Common import foo1

And you also need a __init__.py in all your folders.

๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-lib-path
Understanding and Managing Python Library Paths - CodeRivers
February 22, 2026 - Tools like venv (built-in since Python 3.3) and virtualenv create a self-contained Python environment with its own set of installed libraries and library paths. # Create a virtual environment using venv python3 -m venv myenv # Activate the virtual environment source myenv/bin/activate # On Linux and macOS myenv\Scripts\activate # On Windows # Install libraries within the virtual environment pip install numpy # When the virtual environment is active, Python will look for libraries within the virtual environment's library path
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python-packages-installed-linux
Where Are Python Packages Installed in Linux โ€“ Linux Hint
Local Python packages are installed under /home/<user>/.local/lib/python<version>/. Whatever is your Linux distribution, you can always use the find command to search the files by type, in this case to find the Python packages using its .py extension as shown in the following figure where /usr is the parent directory in the recursive search, -type f defines that you are searching files and not directories, and โ€œ*.pyโ€ is the extension of files that you are looking for.
๐ŸŒ
Real Python
realpython.com โ€บ videos โ€บ module-search-path
The Module Search Path (Video) โ€“ Real Python
Is it possible to make the path.append() call permanent on the python path? Very interesting! Chris Bailey RP Team on Feb. 15, 2020 ยท Hi @Pygator, I guess is depends on what you are looking for. It is possible to make an addition to the PYTHONPATH environment variable. I just did an experiment, where I added a directory on my desktop to the PYTHONPATH and was able to import from it. That would be done inside of the .bash_profile on a mac, and probably Linux.
Published: January 28, 2020