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
Answer from Shawn Chin on Stack OverflowYou 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.
The 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.
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])
(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.
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.
If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.
superuser.com may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.