How to tell Python to prefer a module from $ HOME / lib / python via / usr / lib / python?

In Python, I get an error because it loads the module from /usr/lib/python2.6/site-packages , but I would like it to use my version in $HOME/python-modules/lib/python2.6/site-packages that 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? Installing PYTHONPATH in $HOME/python-modules/lib/python2.6/site-packages does not help, since /usr/lib/... seems to take precedence.

+6
source share
4 answers

Take a look at the site module to configure your environment.

One way to achieve this is to add the file to the location currently located on sys.path called usercustomize.py , when you start Python, it automatically imports the file, and you can use it to modify sys.path .

First set $PYTHONPATH to $HOME (or add $HOME if $PYTHONPATH matters), then create the $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 directory of site directories up to the system default sys.path .

+7
source

Newer versions of Python now have built-in support for searching the opendesktop directory:

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

If you place your local modules there, you do not need any manipulation of sys.path.

+7
source

If you have several versions of an installed package, say, for example, SciPy:

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

and I would like the user version (installed, for example, using pip install --user --upgrade scipy ) to be preferable, requires the usercustomize.py file in ~/.local/lib/python3.5/site-packages/ , for example 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 selection index ensures that packages installed in "development mode" take precedence), now we get our custom 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 
+1
source

to prefer packages installed for the user base (e.g. pip install --user --upgrade cool_thing )

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

 export PYTHONUSERBASE="$HOME/python-modules" 

in $PYTHONUSERBASE/usercustomize.py

 #!/usr/bin/env python import sys, site sys.path.insert(0, site.getusersitepackages()) 
0
source

Source: https://habr.com/ru/post/911994/


All Articles