Python Virtual Environments Explained

python_tutorials

What is VirtualEnv?

The virtualenv tool creates an isolated Python environment (in the form of a directory) that is completely separate from the system-wide Python environment.

What this really means is that any settings, 3rd-party packages, etc. from the system-wide environment do not appear in the virtual environment, so it’s almost like you have a clean Python install.

This is useful for when you want to have a clean-slate for your projects. Let’s say you have boto version 2.7.0 installed in the site-packages, but the project you’re just starting needs the newer 2.38.0 version. Since you can’t have both versions installed site-wide, so you need a Python environment that will keep the dependencies separated. This is what the virtualenv tool is for.

Why is VirtualEnv Useful?

Python is unlike other more enterprise-friendly languages (like Java) in that 3rd-party libraries are loaded and used throughout the whole system, instead of on a project-by-project basis. This can become a problem if two different projects require different versions of the same package.

So for each project you start, you can also create a new virtual environment to ensure that all of the installed dependencies don’t affect the other projects on your computer.

As you create more and more projects, and as you deploy those projects, you’ll soon realize how important it is to have strict separations between projects.

How do you use VirtualEnv?

Virtual environments are easy to create (and destroy), only requiring the virtualenv package, which can be installed with:

$ pip install virtualenv

To create a new virtual environment, you’ll likely want to do something like this:

$ virtualenv --no-site-packages myapp

This command will create the following directory structure:

Using the --no-site-packages flag creates a virtual environment that resembles a clean Python install, and which contains no 3rd-party packages, but only the standard Python packages.

The three sub-directories listed above contain all of the Python executables, dependencies, and packages required to develop and run Python programs. It also includes some useful tools like pip and easy_install.

And finally, in order to use a particular virtual environment, activate it with:

Scotts-Computer:Projects: scott$ cd myapp/
Scotts-Computer:myapp scott$ source bin/activate
(myapp)Scotts-Computer:myapp scott$ 

Notice that this prefixes your command prompt with the name of the virtualenv ((myapp) in this case), which indicates that your current Python environment is the “myapp” virtual environment.

Now every time you run a Python script, the virtual environment’s Python executable, settings, and packages will be used instead of the global Python executable.

To stop using the virtual environment, simply deactivate it by running:

(myapp)Scotts-Computer:myapp scott$ deactivate
Scotts-Computer:myapp scott$

Conclusion

In Python, and just about every other programming language, it is important to have full control over your environment so you know exactly what is going on with your code and how to replicate it on any machine.

Virtual environments help you do this by separating out global configurations and code from local code.

Visit source site

Leave a Reply