Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

module

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.

like image 734
Frank Avatar asked Mar 29 '12 23:03

Frank


People also ask

How do I change the path of a Python site package?

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.

How do you assign a path to a Python module?

Place module.py in the folder where the program will execute. Include the folder that contains the module.py in the PYTHONPATH environment variable. Or you can place the module.py in one of the folders included in the PYTHONPATH variable. Place the module.py in one of the installation-dependent folders.

Does Python use libraries or modules?

The Python Standard Library contains hundreds of modules for performing common tasks, like sending emails or reading JSON data. What's special about the Standard Library is that it comes bundled with your installation of Python, so you can use its modules without having to download them from anywhere.

What is the difference between Python's module package and library?

It is a reusable chunk of code that we can use by importing it into our program, we can just use it by importing that library and calling the method of that library with a period(.). However, it is often assumed that while a package is a collection of modules, a library is a collection of packages.


2 Answers

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.

like image 194
Andrew Clark Avatar answered Oct 21 '22 02:10

Andrew Clark


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.

like image 24
Keith Avatar answered Oct 21 '22 04:10

Keith