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.
(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.
just put the folder in site-packages directory. ie:
C:\PythonXY\Lib\site-packages
Note: you need to add an empty file __init__.py to the folder
Files named __init__.py are used to mark directories on disk as a Python package directories.
If you have the files:
C:\PythonXY\Lib\site-packages\<my_library_folder>\__init__.py
C:\PythonXY\Lib\site-packages\<my_library_folder>\module.py
you can import the code in module.py as:
from <my_library_folder> import module
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
If you have lots of folders, then create the empty __init__.py file in each folder. for eg:
C:\PythonXY\Lib\site-packages\<my_library_folder>\
__init__.py
module.py
subpackage\
__init__.py
submodule1.py
submodule2.py
Set PYTHONPATH environment variable to D:/my_library_folder/
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.
(Spyder maintainer here) We forbid adding site-packages directories through our PYTHONPATH manager because it allows people to mix two different Python versions (which is what you're trying to do by adding your system site-packages to your Anaconda's Python).
And we do that because it usually generates odd errors and segfaults for binary packages such as Numpy, Pandas and Matplotlib, given that binary packages for one Python version are incompatible with packages for another one.
Finally, even though you found a workaround for that (by using sys.path), we strongly suggest you to stop doing that because it'll give you nothing by headaches in the future.
Doing what you are asking isn't the recommended path forward but you can solve the underlying problem in either of the following ways (A or B).
To "ensure pip installs packages to another location which Spyder can see" as the asker guessed in a comment on the accepted answer which got no answer (Method B below) is usually not a good idea. Keeping a clean environment for Spyder will ensure that you can determine requirements (including package version) for each of your projects reliably. Therefore, do the reverse of what you guessed: Ensure Spyder uses the Python interpreter in the environment where pip installed your project's required packages.
A. Change the Python interpreter
- Go to Tools, Preferences, and set Python interpreter to the python executable that was used to install the package (If using a virtual environment, it would be your_other_env/bin/python).
- Close and reopen Spyder (Spyder says to restart the IPython console, but it may not work in this case and show the error where Spyder cannot restart a kernel it didn't start).
- Open Spyder again and run any py file. You will get an error that says to install the
spyder-kernelspackage (for some reason pip 22.0.4 will only installspyder_kernels: This issue is at "spyder-kernels should be spyder_kernels" :edit: but the issue is invalid, so upgrade pip first such as viapip install --upgrade pipin your virtual environment). Take note of the version in the error, since that is the version you need. - If you are using conda or are on Windows the instructions will differ, so see Common Illnesses in the Spyder documentation instead of continuing this step.
source your_other_env/bin/activate
pip install --upgrade pip setuptools
pip install spyder-kernels=...
deactivate
but change ... to the version shown in your Spyder error from step 3. If you installed Spyder with conda as recommended, use the commands from the URL above instead.
I don't recommend Method B, as I've explained. However, it may be useful if you are manually installing Spyder plugins or test suites that apply to all projects but aren't in the requirements.txt or setup.py requirements for your project(s) (and therefore don't affect determining requirements for your users).
B. To "ensure pip installs packages to another location which Spyder can see" you would run "spyder_env/bin/python -m pip install ..." to install the package, where spyder_env is the virtualenv where Spyder is installed (but if Spyder is installed in the system using an installer or linux distro package, you may need to use your system's python such as via python3 -m pip install --user ... where ... is the package name. Always use --user instead of sudo or root to avoid mismatched files caused by mashing together the distro-packaged modules and your manually installed modules).