Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When/how to change python sys.prefix from /usr when site-packages is in /usr/local?

If I'm following https://docs.python.org/2/library/site.html correctly, I need to either move the site-packages directory to /usr/lib/python2.7 or change sys.prefix to /usr/local.

The former seems wrong. For the latter the options I can find are to edit site.py directly or to re-install python. Is editing site.py considered too hacky, or is it a standard-ish thing to do? (ETA: I would think it's a standard thing to do, as that's what it's for. Guess I'm really asking if that's the best choice in this instance.)

Or am I overlooking another option?

/usr/lib vs /usr/local/lib:

auto@virgo:/etc/apache2$ ls -ld /usr/lib/python2.7/site-packages
ls: cannot access /usr/lib/python2.7/site-packages: No such file or directory

auto@virgo:/etc/apache2$ ls -ld /usr/local/lib/python2.7/site-packages
drwxrwsr-x 2 root staff 4096 Aug 29  2013 /usr/local/lib/python2.7/site-packages

python sys.prefix:

auto@virgo:~$ python
Python 2.7.3 (default, Apr 10 2013, 05:46:21) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.prefix
/usr

Thanks!

like image 815
Scott Avatar asked Aug 30 '14 14:08

Scott


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.

What is SYS prefix in Python?

sys. prefix refers to the parent directory of the interpreter installation. It usually includes bin and lib directories for executables and installed modules, respectively.

Where is Python's sys path initialized from?

If the file lib/python<version>/os.py is is found in the directory or any of its subdirectories, that directory is set to be sys.

Where does Python look for site packages?

Default value is ~/. local/lib/pythonX.Y/site-packages for UNIX and non-framework macOS builds, ~/Library/Python/X.Y/lib/python/site-packages for macOS framework builds, and %APPDATA%\Python\PythonXY\site-packages on Windows.


1 Answers

Create either sitecustomize.py or usercustomize.py and append to site.PREFIXES

import site
SITEPKGS = '/usr/local/lib/python2.7/site-packages'
site.addsitedir(SITEPKGS)
site.PREFIXES += ['/usr/local']

assuming /usr/local/lib/python2.7/site-packages is the path to your /usr/local site packages.

From site docs:

After these path manipulations, an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an ImportError exception, it is silently ignored. If Python is started without output streams available, as with pythonw.exe on Windows (which is used by default to start IDLE), attempted output from sitecustomize is ignored. Any exception other than ImportError causes a silent and perhaps mysterious failure of the process.

After this, an attempt is made to import a module named usercustomize, which can perform arbitrary user-specific customizations, if ENABLE_USER_SITE is true. This file is intended to be created in the user site-packages directory (see below), which is part of sys.path unless disabled by -s. An ImportError will be silently ignored.

Note: Where usercustomize.py should go depends on the value of site.USER_SITE, which on Linux for Python-2.7 is ~/.local/lib/python2.7/site-packages.

Note: Also sitecustomize.py would be placed by an administrator in the site packages folder found in sys.prefix.

Other options would be to set environment variables:

  • set PYTHONUSERBASE to /usr/local which will switch the location of the user's base Python directory from ~/.local to whatever you set it to. Note: This would disable your packages installed into ~/.local/lib/python2.7/site-packages.

      export PYTHONUSERBASE=/usr/local
    
  • set PYTHONPATH to prepend /usr/local/lib/python2.7/site-packages which would add it to sys.path.

      export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
    

Environment variables can be exported in your .bashrc or .bash_profile settings, added to /etc/environment to make them system-wide, or you can simple precede your Python interpreter call with the desired environment variables:

$ PYTHONPATH=/usr/local/lib/python2.7/site-packages python
like image 56
Mark Mikofski Avatar answered Oct 03 '22 08:10

Mark Mikofski