9

In Python, I'm getting an error because it's loading a module from /usr/lib/python2.6/site-packages but I'd like it to use my version in $HOME/python-modules/lib/python2.6/site-packages, which I installed using pip-python --install-option="--prefix=$HOME/python-modules --ignore-installed

How can I tell Python to use my version of the library? Setting PYTHONPATH to $HOME/python-modules/lib/python2.6/site-packages doesn't help, since /usr/lib/... apparently has precedence.

2
  • 2
    have you tried just prepending the path to your version of the library before the import? i.e. import sys; sys.path.insert(0,"/path/to/your/version"); import something
    – bjarneh
    Commented Mar 29, 2012 at 23:17
  • I have never done this my self, but I think you could use imp.find_module() and imp.load_module(). Documentation
    – malbani
    Commented Mar 29, 2012 at 23:27

4 Answers 4

10

Take a look at the site module for ways to customize your environment.

One way to accomplish this is to add a file to a location currently on sys.path called usercustomize.py, when Python is starting up it will automatically import this file, and you can use it to modify sys.path.

First, set $PYTHONPATH to $HOME (or add $HOME if $PYTHONPATH has a value), then create the file $HOME/usercustomize.py with the following contents:

import sys, os
my_site = os.path.join(os.environ['HOME'],
                       'python-modules/lib/python2.6/site-packages')
sys.path.insert(0, my_site)

Now when you start Python you should see your custom site-packages directory before the system default on sys.path.

9

Newer Python versions now have built-in support to search the opendesktop location:

$HOME/.local/lib/pythonX.Y/site-packages

If you put your local modules there you don't have to any sys.path manipulations.

3

If one has multiple versions of a package installed, say e.g. SciPy:

>>> import scipy; print(scipy.__version__); print(scipy.__file__)
0.17.0
/usr/lib/python3/dist-packages/scipy/__init__.py

and one would like the user installed version (installed e.g. using pip install --user --upgrade scipy) to be prefered, one needs a usercustomize.py file in ~/.local/lib/python3.5/site-packages/ with e.g. this content:

import sys, os
my_site = os.path.join(
    os.environ['HOME'], '.local/lib/python%d.%d/site-packages' % (
        sys.version_info[0], sys.version_info[1]))
for idx, pth in enumerate(sys.path):
    if pth.startswith('/usr'):
        sys.path.insert(idx, my_site)
        break
else:
    raise ValueError("No path starting with /usr in sys.path")

(the for loop selecting index ensures that packages installed in "develop mode" takes precedence) now we get our user specific version of SciPy:

>>> import scipy; print(scipy.__version__); print(scipy.__file__)
0.18.1
/home/user/.local/lib/python3.5/site-packages/scipy/__init__.py
0

to prefer packages installed to userbase (e.g. pip install --user --upgrade cool_thing )

in ~/.bashrc,~/.profile, or whatever the init file for your shell is, add

export PYTHONUSERBASE="$HOME/python-modules"

in $PYTHONUSERBASE/usercustomize.py

#!/usr/bin/env python
import sys, site
sys.path.insert(0, site.getusersitepackages())
1
  • I think the correct location for the file is $PYTHONUSERBASE/lib/pythonX.X/site-packages/usercustomize.py Commented Mar 24, 2023 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.