Python's virtualenv sandbox environment

Source: Internet
Author: User
Tags virtual environment 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:

# CentOS7.2 Release version Yum Install epel-release-yyuminstall python-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 MYPROJECT01CD myproject01/

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

Virtualenv--no-site-packages venv  #参数--no-site-packages  All third-party packages that have been installed into the system Python environment will not replicate, so that We get a "clean" python run environment without any third-party packages.

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:

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.

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

Install django==1.11

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:

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.

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

Question: What if there are python2 and Python3 in a set of environments?

First of all, you need to install Python2 and Python3 in your CentOS7.2.

Http://www.cnblogs.com/anpengapple/p/5907416.html

Virtualenv can go to https://pypi.python.org/pypi/virtualenv to download. After downloading the decompression, then my practice is to use the CP copy to the/usr/local directory, copy two, respectively named Virtualenv2 and Virtualenv3, and

Add a 3 to the first line and change the python to Python3,

Next do two soft connections:

Ln -s/usr/local/virtualenv2/virtualenv.py/usr/sbin/virtualenv2Ln -s/usr/local/ Virtualenv3/virtualenv.py/usr/sbin/virtualenv3

Well, this next Python2 and 3 of the virtualenv are done, can be used directly. I have recently used the PYTHON3 environment, so the virtual environment is only python3.

To do the experiment, we used a test directory

mkdirenvenv/bin/activate# If you run out, you just need to use deactivate

If you want to use Python2, you can virtualenv2--no-site-packages env2

Python's virtualenv sandbox environment

Related Article

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.