51 Common third-party modules Virtualenv

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:

$ PIP3 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:

mac:~ michael$ mkdir myprojectmac:~ michael$ CD myproject/mac:myproject michael$

The second step is to create a separate Python runtime environment named venv :

Mac:myproject michael$ virtualenv--no-site-base'/usr/local/.../python.framework/ versions/3.4' in Venv/bin/python3. 4   in venv/bin/pythoninstalling setuptools, Pip, Wheel...done.

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, you can source enter the environment:

Mac:myproject michael$ Source venv/bin/a

Notice that the command prompt is changed and has a (venv) prefix that indicates that the current environment is a venv python environment named.

The following normal installation of various third-party packages, and run the python command:

(venv) mac:myproject michael$ pip Install jinja2 ... Successfully installed JINJA2-2.7. 3 markupsafe-0.23(venv) mac:myproject michael$ python myapp.py
In the VENV environment, packages installed with PIP are installed into the VENV environment, and the system Python environment is unaffected. That is, the VENV environment is created specifically for the MyProject application. To exit the current VENV environment, use the Deactivate command:
(venv) Mac:myproject michael$ Deactivate Mac:myproject michael$

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.

Summary

VIRTUALENV provides an isolated Python runtime environment for applications that resolves multiple versions of conflicts across different applications.

51 Common third-party modules Virtualenv

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.