How to set site-packages directory for local Python 2.7 install
Question: I'm trying to run a certain script in Python, but it requires some other
modules (setuptools) - I don't have write permissions for our /usr/ directory
to install them, so I'm trying to install a local version of Python 2.7 to run
it (not in /usr/).

When I try to run the ez_setup script for setuptools, it tries to access:

> /usr/local/lib/python2.7/site-packages/

But that's not the installation I want (I get write permission error). I can
point the ez_setup script to wherever, but I'm not sure how to get Python to
use it. In my 2.7 install I ran:

> site.getsitepackages()

And got ['/usr/local/lib/python2.7/site-packages', '/usr/local/lib/site-
python'].

Is there a way to change the default site-packages directory so I can do local
installs? Like in the Python installation directory itself?

Thanks, Kaleb


Answer: You really want to read up on Python [virtual
environments](http://docs.python-guide.org/en/latest/dev/virtualenvs/), which
allow you to create a "local" Python tree into which you can install your own
packages and such without requiring root access.

For example, assuming that you have the `virtualenv` command available (you
may need to install this first; it's available as a package for most major
distributions), you can create a new virtual environment like this:

    
    
    virtualenv --system-site-packages myenv
    

The `--system-site-packages` option will make any packages in your system
site-packages directory visible to this environment, as well as anything you
install locally. Then activate the environment:

    
    
    . myenv/bin/activate
    

And install stuff:

    
    
    pip install apackage
    

**NB** For no good reason, `pydoc` will stop working for you in this
configuration. You can just drop a new `pydoc` script into `myenv/bin/pydoc`
that looks like this:

    
    
    #!/usr/bin/env python
    
    import pydoc
    if __name__ == '__main__':
        pydoc.cli()
    

