Original: http://www.nowamagic.net/academy/detail/1330228
Different people like to build their own development environment in different ways, but in almost all programming communities, there is always one (or more) development environment that makes it easier for people to accept. There are no errors in using different development environments, but some environment settings make it easier to test easily and do repetitive/templated tasks that make daily routines simple and easy to maintain.
What is Virtualenv?
The most common method in a python development environment is to use the VIRTUALENV package. Virtualenv is a package that is used to create a standalone Python environment. Now, there's the question: Why do we need a standalone Python environment? To answer this question, allow me to cite Virtualenv's own documentation:
Virtualenv is a tool to create isolated Python environments.
The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you has an application that needs version 1 of Libfoo, but another application requires version 2. How can I use both these applications? If You install everything into/usr/lib/python2.7/site-packages (or whatever your platform's), it ' s E Asy to end of a situation where you unintentionally upgrade an application that shouldn ' t is upgraded.
Or more generally, what if you want to install a application and leave it be? If An application works, the libraries or the versions of those libraries can break the application.
Also, what if 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 the have its own installation directories, which doesn ' t share libraries with other virtualenv en Vironments (and optionally doesn ' t access the globally installed libraries either).
The basic problem that we need to deal with is the dependency, version, and indirect permissions issues of the package. Imagine that you have two apps, one app requires Libfoo version 1, and another app requires version 2. How can I use these applications at the same time? If you install everything to the/usr/lib/python2.7/site-packages (or any platform's standard location) in this case, you may accidentally upgrade an application that should not be upgraded.
Simply put, you can create a different/separate Python environment for each project, and you will install all the required packages into their own separate environments for each project.
Installation and use of virtualenv
Installing the virtualenv is simple:
Once the virtualenv is installed, you can create a separate Python environment for your project by running the following command:
1 |
virtualenv --distribute nowamagic_venv |
OK, success. What happened up there? He created the folder nowamagic_venv to store your new standalone Python environment. This folder is located under/root.
Let's take a look at the output:
1 |
New python executable in nowamagic_venv/bin/python2.7 |
2 |
Also creating executable in nowamagic_venv/bin/python |
3 |
Installing Setuptools...... done . |
4 |
Installing Pip........... done . |
The--distribute option enables virtualenv to use the new release-based package management system rather than the Setuptools-acquired package. All you need to know now is that the--distribute option automatically installs PIP in the new virtual environment, so you don't need to install it manually. When you become a more experienced Python developer, you will understand the details.
- Activate: This virtualenv activation file
- PIP: This virtualenv's independent pip
- A copy of the Python:python interpreter
- lib/python2.7: All new packages will be present in this
Test it.
Activate this virtualenv by using the following command:
1 |
[[email protected] ~] # cd nowamagic_venv |
2 |
[[email protected] nowamagic_venv] # source bin/activate |
3 |
(nowamagic_venv)[[email protected] nowamagic_venv] # |
Run the following command to better understand the difference between the two, if you have entered the virtualenv please leave first.
First let's see if it calls the PYTHON/PIP command and it calls that one.
1 |
[[email protected] ~] # which python |
1 |
[[email protected] ~] # which pip |
One more time! Open the Virtualenv this time and see what's different. The following is shown on my machine:
1 |
[[email protected] ~] # which python |
2 |
/root/nowamagic_venv/bin/python |
1 |
[[email protected] ~] # which pip |
2 |
/root/nowamagic_venv/bin/pip |
Virtualenv copies a copy of the Python executable file, creates useful scripts and installs the required packages for the project, and you can install/upgrade/remove them throughout the project's lifecycle. It also modifies some search paths, such as Pythonpath, to ensure that:
- When packages are installed, they are installed in the currently active virtualenv, not in the system-wide Python path.
- When the import code occurs, VIRTUALENV will take precedence over the packages installed in the environment, rather than the packages installed in the System Python directory.
It is also important that, by default, all system-wide packages are visible to virtualenv. This means that if you install Simplejson in your system python directory, it will automatically be available to all Virtualenvs for use. This behavior can be changed, the virtualenv of adding--no-site-packages option when creating virtualenv will not read the system package, as follows (this should be said in the Site-packages directory of the package, For ubuntu12.04, this directory is empty, so virtualenv never uses a third-party package in the System Python directory. Note: Ubuntu12.04 's third-party package is in the Dist-packages directory):
1 |
virtualenv nowamagic_venv --no-site-packages |
Build multiple Python independent development environments with VIRTUALENV (GO)