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.gztar 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_HOMEsource ~/.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 :-)
If you follow the steps advised in several tutorials I linked in this answer, youcan get the desired effect without the somewhat complicated "manual" steps in Walker's and Vinay's answers. If you're on Ubuntu:
sudo apt-get install python-pip python-dev
The equivalent is achieved in OS X by using homebrew to install python (more details here).
brew install python
With pip
installed, you can use it to get the remaining packages (you can omit sudo
in OS X, as you're using your local python installation).
sudo pip install virtualenvwrapper
(these are the only packages you need installed globally and I doubt that it will clash with anything system-level from the OS. If you want to be super-safe, you can keep the distro's versions sudo apt-get install virtualenvwrapper
)
Note: in Ubuntu 14.04 I receive some errors with pip install, so I use pip3 install virtualenv virtualenvwrapper
and add VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
to my .bashrc/.zshrc
file.
You then append to your .bashrc
file
export WORKON_HOMEsource /usr/local/bin/virtualenvwrapper.sh
and source it
. ~/.bashrc
This is basically it. Now the only decision is whether you want to create a virtualenv to include system-level packages
mkvirtualenv --system-site-packages foo
where your existing system packages don't have to be reinstalled, they are symlinked to the system interpreter's versions. Note: you can still install new packages and upgrade existing included-from-system packages without sudo - I tested it and it works without any disruptions of the system interpreter.
kermit@hocus-pocus:~$ sudo apt-get install python-pandaskermit@hocus-pocus:~$ mkvirtualenv --system-site-packages s(s)kermit@hocus-pocus:~$ pip install --upgrade pandas(s)kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"0.10.1(s)kermit@hocus-pocus:~$ deactivatekermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"0.8.0
The alternative, if you want a completely separated environment, is
mkvirtualenv --no-site-packages bar
or given that this is the default option, simply
mkvirtualenv bar
The result is that you have a new virtualenv where you can freely and sudolessly install your favourite packages
pip install flask