Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Today I saw an example of how virtualenv command is used. It was the following four commands:

virtualenv /tmp/vetest
source /tmp/vetest/bin/activate
pip install ipython-notebook
deactivate

The first command creates /tmp/vetest/ directory in which there are four subdirectories: lib, include, local, bin.

Then we use source. What does it do? As far as I understand it puts me into an virtual environment. Being there allows me to install different stuff (I cannot do it otherwise since I do not have root permissions).

With deactivate I leave the virtual environment. This is what I understand so far.

Few questions remain unclear to me. What this command has to do with Python? Is this command a "standard" for Unix systems? Does virtualenv does something more that just creating new directories? What is /tmp/ directory?

share|improve this question
add comment

1 Answer

Surprisingly, googling "virtualenv" leads you to the virtualenv documentation. If you want to use it, I suggest at least a quick read-through.

As the name suggests, virtualenv is a tool to create isolated or "virtual" environments for Python. It allows you to set up multiple independent instances for different projects, each with their own modules and even versions of Python. This is useful in a variety of circumstances, not least when you don't have admin access and want to install Python modules. From the docs:

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

So there you have it. lib/ is where the modules live. include/ is for headers and other shared stuff. local/ is for stuff that lives outside of the main site-packages`` (inlib/) module home, like your own applications. And finally,bin/` is where executables live.

The /tmp directory is, just as it sounds, a temp directory - used for storing things temporarily. Depending on the system, they may or may not be removed periodically, or when the system is rebooted. Or not, it depends. Your first command created a subdirectory of /tmp called vetest/.

Your second command, source, basically means "run the commands listed in this file". It is a built-in command, part of the shell. The result of this command is to start the virtual environment. You should now see (venv) prefixed to your shell prompt.

Once inside, you used the Python Installer Program, or pip, which is automatically included in every virtualenv. pip communicates with the Python Package Index or PyPI. This is the closest thing the Python community has to a central repository, similar to Perl's CPAN or Ruby's rubygems.org. In your case, pip was looking for the ipython-notebook module, which unfortunately does not exist (the notebook is part of the core IPython installation). Had you run pip install ipython you would have gotten something along the lines of:

Downloading/unpacking ipython
  Downloading ipython-0.13.2.zip (6.4MB): 6.4MB downloaded
  Running setup.py egg_info for package ipython

Installing collected packages: ipython
  Running setup.py install for ipython

    Installing ipcluster3 script to /tmp/vetest/bin
    Installing irunner3 script to /tmp/vetest/bin
    Installing ipcontroller3 script to /tmp/vetest/bin
    Installing iptest3 script to /tmp/vetest/bin
    Installing pycolor3 script to /tmp/vetest/bin
    Installing iplogger3 script to /tmp/vetest/bin
    Installing ipengine3 script to /tmp/vetest/bin
    Installing ipython3 script to /tmp/vetest/bin

Successfully installed ipython
Cleaning up...

From /tmp/vetest, you could now type ipython3 notebook (assuming you have Python 3, which you should be using anyways) to start the IPython Notebook server, and a session in your browser. Unfortunately, it will fail because you lack a bunch of dependencies like tornado, but these are easily installed using pip. A list of basic dependencies is available on the IPython website. Additional functionality like pylab requires NumPy, SciPy, and matplotlib.

Finally, the deactivate command exits your virtualenv session.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.