Using virtualenv to provide an isolated Python run environment for your application

Source: Internet
Author: User
Tags virtualenv

When developing a Python application, the system installs only one version of the Python3:3.4. All third-party packages will be pip installed in the Python3 site-packages directory.

If we were to develop multiple applications at the same time, those applications would share a python, which is the Python 3 installed in the system. What if application a requires Jinja 2.7 and application B needs Jinja 2.6?

In this case, each app may need to have a "standalone" Python runtime environment. Virtualenv is a python run environment that is used to create a "quarantine" for an application.

First, we use the pip installation virtualenv:

Pip Install Virtualenv

Then, assuming that we are going to develop a new project, we need a separate Python runtime environment to do this:

The first step is to create the directory:

mkdir MYPROJECTCD MyProject

The second step is to create a separate Python runtime environment namedvenv

Virtualenv--no-site-packages venv

Command virtualenv to create a standalone Python runtime environment, we have added parameters --no-site-packages so that all third-party packages that have been installed into the system Python environment will not replicate, so that we get a "clean" without any third-party packages Python runtime environment.

The new Python environment is placed in the directory under the current directory venv . With venv This Python environment, LINUX/MAC can be used to enter source the environment, using venv/script/activate under window:

SOURCE Venv/bin/activate

Notice that the command prompt is changed and has a (venv) prefix that indicates that the current environment is a venv python environment named. You can then use the PIP command to install the various packages.

Under the environment, the system Python environment is not affected by the installation of the packages installed in venv pip venv this environment. In other words, the venv environment is created specifically for myproject this application.

To exit the current venv environment, use the deactivate command:

Deactivate

At this point, we are back to normal environment, now pip or python all in the system Python environment.

It is entirely possible to create a separate Python runtime environment for each app, so that each app's Python environment can be isolated.

How does virtualenv create a "stand-alone" Python runtime environment? The principle is simple, is to copy the system Python to virtualenv environment, with the command source venv/bin/activate into a virtualenv environment, VIRTUALENV will modify the relevant environment variables, let the command python and pip All point to the current virtualenv environment.

Using virtualenv to provide an isolated Python run environment for your application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.