What's the proper way to install pip, virtualenv, and distribute for Python?
You can do this without installing anything into python itself.
You don't need sudo or any privileges.
You don't need to edit any files.
Install virtualenv into a bootstrap virtual environment. Use the that virtual environment to create more. Since virtualenv ships with pip and distribute, you get everything from one install.
- Download virtualenv:
- http://pypi.python.org/pypi/virtualenv
- https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz (or whatever is the latest version!)
- Unpack the source tarball
- Use the unpacked tarball to create a clean virtual environment. This virtual environment will be used to "bootstrap" others. All of your virtual environments will automatically contain pip and distribute.
- Using pip, install virtualenv into that bootstrap environment.
- Use that bootstrap environment to create more!
Here is an example in bash:
# Select current version of virtualenv:
VERSION=12.0.7
# Name your first "bootstrap" environment:
INITIAL_ENV=bootstrap
# Set to whatever python interpreter you want for your first environment:
PYTHON=$(which python)
URL_BASE=https://pypi.python.org/packages/source/v/virtualenv
# --- Real work starts here ---
curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
tar xzf virtualenv-$VERSION.tar.gz
# Create the first "bootstrap" environment.
$PYTHON virtualenv-$VERSION/virtualenv.py $INITIAL_ENV
# Don't need this anymore.
rm -rf virtualenv-$VERSION
# Install virtualenv into the environment.
$INITIAL_ENV/bin/pip install virtualenv-$VERSION.tar.gz
Now you can use your "bootstrap" environment to create more:
# Create a second environment from the first:
$INITIAL_ENV/bin/virtualenv py-env1
# Create more:
$INITIAL_ENV/bin/virtualenv py-env2
Go nuts!
Note
This assumes you are not using a really old version of virtualenv.
Old versions required the flags --no-site-packges
(and depending on the version of Python, --distribute
). Now you can create your bootstrap environment with just python virtualenv.py path-to-bootstrap
or python3 virtualenv.py path-to-bootstrap
.
I think Glyph means do something like this:
- Create a directory
~/.local
, if it doesn't already exist. - In your
~/.bashrc
, ensure that~/.local/bin
is onPATH
and that~/.local
is onPYTHONPATH
. -
Create a file
~/.pydistutils.cfg
which contains[install] prefix=~/.local
It's a standard ConfigParser-format file.
-
Download
distribute_setup.py
and runpython distribute_setup.py
(nosudo
). If it complains about a non-existingsite-packages
directory, create it manually:mkdir -p ~/.local/lib/python2.7/site-packages/
Run
which easy_install
to verify that it's coming from~/.local/bin
- Run
pip install virtualenv
- Run
pip install virtualenvwrapper
- Create a virtual env containing folder, say
~/.virtualenvs
-
In
~/.bashrc
addexport WORKON_HOME source ~/.local/bin/virtualenvwrapper.sh
That's it, no use of sudo
at all and your Python environment is in ~/.local
, completely separate from the OS's Python. Disclaimer: Not sure how compatible virtualenvwrapper
is in this scenario - I couldn't test it on my system :-)