Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does virtualenv work?

I checked the activate script and it looks to me all it does is:

  • set VIRTUAL_ENV env
  • append $VIRTUAL_ENV/bin in front of PATH

How does virtualenv provide that magical virtual environment by these? What do I miss?

like image 990
Drake Guan Avatar asked Dec 08 '11 07:12

Drake Guan


People also ask

How does a Python virtual environment work?

A virtual environment is simply a tool that separates the dependencies of different projects by creating a separate isolated environment for each project. These are simply the directories so that unlimited virtual environments can be created. This is one of the popular tools used by most of the Python developers.

What is virtualenv and how do you use it?

virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.

Why do we use virtualenv?

virtualenv allows you to avoid installing Python packages globally by making an isolated python environment. That means it will install packages just in your desire project folder.


2 Answers

I will describe the basic process, which I learned from the presentation @jcollado linked to.

When Python starts, it looks at the path of the binary, and the prefixes thereof.

So let's say your virtualenv is /home/blah/scratch. The Python process knows it was executed from /home/blah/scratch/bin/python (which is usually just a copy of your system python binary /usr/bin/python) and it knows its own version X.Y because it's compiled into it. Then Python looks for lib/pythonX.Y/os.py in this order:

/home/blah/scratch/bin/lib/pythonX.Y/os.py /home/blah/scratch/lib/pythonX.Y/os.py    <-- this file should exist /home/blah/lib/pythonX.Y/os.py /home/lib/pythonX.Y/os.py /lib/pythonX.Y/os.py 

It stops at /home/blah/scratch/lib/pythonX.Y/os.py because it's the first file that actually exists. If it didn't, Python would keep looking. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

like image 187
Max Avatar answered Sep 22 '22 11:09

Max


  1. First the user creates a new virtualenv with the command virtualenv myenv. This creates a directory called myenv and copies the system python binary to myenv/bin. It also adds other necessary files and directories to myenv, including a setup script in bin/activate and a lib subdirectory for modules and packages.
  2. Then the user sources the activate script with . myenv/bin/activate, which sets the shell’s PATH environment variable to start with myenv/bin.
  3. Now when the user runs python from this shell, it will execute the copy of the binary stored in myenv/bin. Even though the binary is identical to the one in /usr/bin/python, the standard python binary is designed to search for packages and modules in directories that are relative to the binary’s path (this functionality is not related to virtualenv). It looks in ../lib/pythonX.Y where X and Y are the major and minor version numbers of the python binary. So now it is looking in myenv/lib/pythonX.Y.
  4. The myenv/bin directory also contains a script named pip so that when the user installs new packages using pip from the virtualenv, they will be installed in myenv/lib/pythonX.Y
like image 40
clark800 Avatar answered Sep 22 '22 11:09

clark800