The variable PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. This variable is not set by default and not needed for Python to work because it it already knows where to find its standard libraries (sys.path).
But if for any reason you need some custom Python libraries that you do not want to install, you can export PYTHONPATH=/path/to/my/modules/ to add /path/to/my/modules/ to the paths that Python will explore, then it will know where to find those custom modules.
And this time, if you print again sys.path, it will display you the newly added modules.
Answer from kurisukun on Stack OverflowThe site module documentation and Modifying Python's Search Path seem to be what you're looking for.
As far as I understand it, those entries are being added to sys.path by:
/usr/lib/python2.6/site.py/usr/lib/python2.6/dist-packages/site.py
(Change 2.6 to your version of Python.)
The easiest way to change it is to add a file /usr/local/lib/python2.6/dist-packages/site-packages.pth containing ../site-packages.
Alternatively, maybe you can teach the package to use site.getsitepackages()?
I'd like to summarize my findings about python's path modification. There are two ways to do it.
- .pth file
PYTHONPATH
Any .pth file which is found on the default path (see bellow) will get its content included into sys.path.
Format of said .pth file is simple: one (folder) path per line. Surprisingly, the paths can be absolute or relative to the .pth file.
Default path is where the interpreter resides and <some-prefix>/lib/python<version>/site-packages where <some-prefix> is usually /usr/.
PYTHONPATH is environmental variable of your operating system. On unix systems you list them by env. Global modification of such variables is done through .sh scripts inside /etc/profile.d/ folder as mentioned by @TestUser16418.
Usage of PYTHONPATH
What is Python's site-packages directory? - Stack Overflow
suse - Configure Python to include another directory when looking for packages - Unix & Linux Stack Exchange
Configuring Python to use additional locations for site-packages - Stack Overflow
There are two types of site-packages directories, global and per user.
Global site-packages ("dist-packages") directories are listed in
sys.pathwhen you run:python -m siteFor a more concise list run
getsitepackagesfrom the site module in Python code:python -c 'import site; print(site.getsitepackages())'Caution: In virtual environments getsitepackages is not available with older versions of
virtualenv,sys.pathfrom above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'The per user site-packages directory (PEP 370) is where Python installs your local packages:
python -m site --user-siteIf this points to a non-existing directory check the exit status of Python and see
python -m site --helpfor explanations.Hint: Running
pip list --userorpip freeze --usergives you a list of all installed per user site-packages.
Practical Tips
<package>.__path__lets you identify the location(s) of a specific package: (details)$ python -c "import setuptools as _; print(_.__path__)" ['/usr/lib/python2.7/dist-packages/setuptools']<module>.__file__lets you identify the location of a specific module: (difference)$ python3 -c "import os as _; print(_.__file__)" /usr/lib/python3.6/os.pyRun
pip show <package>to show Debian-style package information:$ pip show pytest Name: pytest Version: 3.8.2 Summary: pytest: simple powerful testing with Python Home-page: https://docs.pytest.org/en/latest/ Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others Author-email: None License: MIT license Location: /home/peter/.local/lib/python3.4/site-packages Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
(or just first item with site.getsitepackages()[0])
site-packages is the target directory of manually built Python packages. When you build and install Python packages from source (using distutils, probably by executing python setup.py install), you will find the installed modules in site-packages by default.
There are standard locations:
- Unix (pure)1:
prefix/lib/pythonX.Y/site-packages - Unix (non-pure):
exec-prefix/lib/pythonX.Y/site-packages - Windows:
prefix\Lib\site-packages
1 Pure means that the module uses only Python code. Non-pure can contain C/C++ code as well.
site-packages is by default part of the Python search path, so modules installed there can be imported easily afterwards.
Useful reading
- Installing Python Modules (for Python 2)
- Installing Python Modules (for Python 3)
When you use --user option with pip, the package gets installed in user's folder instead of global folder and you won't need to run pip command with admin privileges.
The location of user's packages folder can be found using:
python -m site --user-site
This will print something like:
C:\Users\%USERNAME%\AppData\Roaming\Python\Python35\site-packages
When you don't use --user option with pip, the package gets installed in global folder given by:
python -c "import site; print(site.getsitepackages())"
This will print something like:
['C:\\Program Files\\Anaconda3', 'C:\\Program Files\\Anaconda3\\lib\\site-packages'
Note: Above printed values are for On Windows 10 with Anaconda 4.x installed with defaults.
(Please correct errors and omissions as necessary. Thanks.)
First, a question and a comment. I don't use SUSE, so take this with a pinch of salt. Are the packages that install in /usr/lib/python2.6/site-packages official packages? If so, SUSE is broken, so that is not likely. If they are not official packages, you could either ask the packagers to use the standard paths, or, alternatively, you could submit a wishlist bug to SUSE asking them to support this additional path. This will save you and other people additional headaches.
For the moment, you have the following possibilities, in order of decreasing scope:
Change the module search path for all users (method 1)
Change the module search path in the Python installation.
The default module search path is hardwired into the binary. Add-on paths can be configured at runtime, for example in the site.py file. For example, Debian uses /usr/lib/python2.6/site.py (for the default python 2.6 installation) to do its site-specific configuration. At the top of the file is written
Append module search paths for third-party packages to `sys.path`. **************************************************************** * This module is automatically imported during initialization. * **************************************************************** [...] This will append site-specific paths to the module search path.The Debian patch
debian/patches/site-locations.diffsaysFor Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python/dist-packages, Debian addons install into /usr/{lib,share}/python/dist-packages. /usr/lib/python/site-packages is not used.
The patch in question is
sitedirs.append(os.path.join(prefix, "Lib", "site-packages")) elif os.sep == '/': + sitedirs.append(os.path.join(prefix, "local/lib", + "python" + sys.version[:3], + "dist-packages")) sitedirs.append(os.path.join(prefix, "lib", "python" + sys.version[:3], - "site-packages")) - sitedirs.append(os.path.join(prefix, "lib", "site-python")) + "dist-packages")) + sitedirs.append(os.path.join(prefix, "lib", "dist-python")) else: sitedirs.append(prefix) sitedirs.append(os.path.join(prefix, "lib", "site-packages"))So you could modify the site.path in your system package to produce a modified module search path. You probably don't want to this, though. For one thing, you will have to merge this in on every update of your distribution's python package.
Change the module search path for all users (method 2)
Add a file of the form something.pth to a directory that is already in the search path, which contains a path, either relative or absolute. Eg.
/usr/local/lib/python2.6/dist-packages$ cat foo.pth /home/faheem/dummypythonpathIn another terminal do
>>> import sys >>> sys.path [...,'/home/faheem/dummypythonpath',...,]Change the module search path for all users (method 3)
The environment variable PYTHONPATH is normally used to append to the system path at user level. You can put it in a file which will be sourced by all users. Eg. in Debian we have /etc/bash.bashrc, which says at the top
# System-wide .bashrc file for interactive bash(1) shells. # To enable the settings / commands in this file for login shells as well, # this file has to be sourced in /etc/profile.So you could add or PYTHONPATH there. You probably want it to be sourced for both login and interactive shells, so you'll want to check on that. Unfortunately, distributions are often flaky about enabling this.
The paths in PYTHONPATH are added to the default list of search paths in the system (which can be obtained for example by sys.path - see below). Allowing for the possibility that PYTHONPATH is set already, just add desired additional directories to it, eg.
export PYTHONPATH=$PYTHONPATH:"/home/username/lib/python2.6/dist-packages"If you source the PYTHONPATH variable, and then check sys.path again, you will
see the paths have been added. Note that the position in which the paths in
PYTHONPATH are added to the pre-existing paths does not seem to be prescribed by
the implementation.Change the module search path per user.
The usual way is to change PYTHONPATH in the user's bashrc, namely
~/.bashrc. Again, check that it is sourced for both login and interactive shells.Change the module search path on a per script basis. This is done by appending to sys.path, namely
import sys sys.path.append(some_additional_path)This will only work for the script that is importing this. This is normally used, as far as I know, for casual use, when importing modules in nonstandard locations, like from somewhere in a home directory.
See also Greg Ward on Modifying Python's Search Path. This has a good discussion of the available alternatives.
Use sys.path:
import sys
sys.path.append("/home/me/mypy")
You can also check the site module documentation, which explains how the site-specific paths are computed.
You can use the Site-specific configuration hook.
"A path configuration file is a file whose name has the form
name.pthand exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added tosys.path."
In your case, you should be able to achieve what you want by simply dropping in a .pth file containing the path of the directory to include:
[root@home]$ echo "/usr/local/lib/python2.7/site-packages/" > /opt/python2.7.2/lib/python2.7/site-packages/usrlocal.pth
You could replace the python executable with a wrapper script which appends your added installpath to PYTHONPATH. But this is a kludge.
But I'd try to fix the installation of the add-on so that it properly goes into the site-packages dir.
Hello, I built and packaged python from source code for my distro (cause the maintainers still don't have the latest version). The built and install was succeed but I need to set PYTHONPATH in order to make some modules get detected. The PYTHONPATH is "/usr/lib/python3.11/:/usr/lib64/python3.11/:/usr/lib/python3.11/site-packages/" . How the PYTHONPATH isn't configured / setted automatically ? And is that normal ? thanks.
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it, Please add
C:\Python27;
along with it. Else it will never work.
Windows 7 Professional I Modified @mongoose_za's answer to make it easier to change the python version:
- [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
- Click [New] under "System Variable"
- Variable Name: PY_HOME, Variable Value:C:\path\to\python\version

- Click [OK]
- Locate the "Path" System variable and click [Edit]
Add the following to the existing variable:
%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;

Click [OK] to close all of the windows.
As a final sanity check open a command prompt and enter python. You should see
>python [whatever version you are using]
If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.