When installing Python software, sudo easy_install and sudo pip are something you should do very seldom. sudo means you are messing with your operating system files. easy_install means that it is easy to install, but impossible to uninstall. You most likely step on the toes your operating system package manager and make your Python installation damaged – damaged in a way you cannot reliably use OS package manager to install or upgrade the software in the future. There is also a high chance that two software depends on the different version of a library X and easy_install will happily overwrite anything with different version in your system.
Even though being badly broken, still plenty of Python software documentation recommends sudo easy_install as an installation method.
virtualenv is a Python tool for creating an isolated Python environment for a normal user. They are isolated in a sense, that only the software you install there will see and mess with the environment. When you are running virtualenv’ed python, pip and easy_install can freely pull in any libraries from pypi without the worry that you break some other software on your computer.
Here is a recipe for installing virtualenv’ed Python software which should across UNIXes and Windows. It will totally run as your normal user privileges, no sudo or admin needed. Only an installed Python interpreter and a working python command are the requirements.
So fire up your console, go the folder where you wish to perform the installation and type the following (some commenting added to explain the process):
# First create the folder where you wish to install the software # with mkdir command. Then go to this folder with cd command. # Windows users: Please manually download virtualenv.py # to the target folder ith your web browser. UNIX users # can use curl command line downloader as below. # Note: Don't rely on operating system virtualenv command. # It might be hassle to instruct virtualenv package installation # due to distribution flavours. # Old Ubuntus ship really old virtualenv.py and it has not worked # on all cases. # Github virtualenv.py is the msot reliable method. curl -L -o virtualenv.py https://raw.github.com/pypa/virtualenv/master/virtualenv.py # Create a virtualenv environment # where the software and its dependencies # will be pulled from PyPi. In our case # we call the created virtualenv folder "venv" python virtualenv.py venv # Activate the virtualenv environment. # This will set your PATH environment # variable so that following "python" # command executes from under the virtualenv, # not from your global system setup. # # Windows equivalent: .\venv\Scripts\activate . venv/bin/activate # Now when virtualenv is activated, # pip and easy_install will install any software # under this virtualenv environment, not on your operating system files pip install YOUR_PACKAGE_NAME_ON_PYPI.PYTHON.ORG # Usually, if you install Python command line software, # new launcher scripts get created in venv/bin # folder. When venv environment is active, # this folder takes precedence in PATH environment # variable. Meaning, when you have virtualenv activated # you can simply type in the installed command name # without full path to execute it.
And again with a real life example:
curl -L -o virtualenv.py https://raw.github.com/pypa/virtualenv/master/virtualenv.py python virtualenv.py vvv-venv . vvv-venv/bin/activate pip install vvv
I have tested this recipe with vvv and Skype sevabot and have found it working. However, I wish to get some feedback and ideas how this could be further enhanced, so please send in your ideas.
Some notes
- Don’t use UNIX shell source command as it is not standard. Use . instead.
- curl is available on OSX by default. wget is alternative, but not available on all systems out of the box.
Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+
While this is decent advice for application specific dependencies, the “–user” option to “pip install“ is a much simpler choice if you just want to install some Python packages for your own use without touching the system Python directories.
Nick: That implies that you have pip installed on your system. Again for that you need to have instructions for OS package manager specific instructions how to get pip on your computer in the first place.
The dot notation for sourcing is not standard, either. For example, it does not work with zsh.
Brian: That is not true. In my ZSH it works fine:
[~/code]% . plone-venv/bin/activate
[plone-venv]
[~/code]%
I thought “source” and “.” are exactly the same. Since “.” is just a synonym for “source” http://ss64.com/bash/period.html
Of course write . is always faster =)
Pingback: psutil – advanced OS process management utilities for Python
Pingback: This documentation was brought you by these awesome persons
Pingback: Accepting and spending bitcoins in a Django application
Pingback: Sublime Text 3 for Python, JavaScript and web developers
@Brian: it’s the other way around: “.” is the standard, “source” is a bash synonym.
@Hieu: “.” may be faster to write, but has *very* poor readability, and it is often overlooked/misnderstood (try to explain a reader that `. ./.file` is NOT a typo 🙂
Code is read way more times than it’s written, so being “faster” to write is usually a bad idea. If bash is known to be available, use “source”
Pingback: Turbocharge your Python prompt and Django shell with IPython Notebook
Pingback: setuptools error python27 setup.py install: Read-only file system | Zangara Answers