Python Virtual Environment virtualenv installation and use, pythonvirtualenv

Source: Internet
Author: User
Tags virtual environment virtualenv

Python Virtual Environment virtualenv installation and use, pythonvirtualenv

When multiple projects are deployed on the same server, different versions of django or other python libraries may be used for the project. In this case, virtualenv can be used to create an independent python runtime environment, isolate the Running Environments of different projects.

Virtualenv can create an environment with its own installation directory, allowing you to conveniently manage python versions and python libraries.

Install

pip install virtualenv

Create Environment

Basic commands:

virtualenv ENV

A new virtual environment is created in the ENV directory.

Pip and setuptools have been automatically downloaded in the virtual environment. You can use these tools to download python packages.

The downloaded python package is placed in ENV/lib/pythonX. X/site-packages.

The currently used python interpreter is ENV/bin/python.

Activate and exit the Virtual Environment

Activation command:

$ source bin/activate

Exit the virtual environment:

$ deactivate

Delete Virtual Environment

After exiting the virtual environment, delete the directory:

(ENV)$ deactivate$ rm -r /path/to/ENV

Inherit the global python Library

Run the following command to create a virtual environment that inherits the python global library under/usr/lib/python2.7/site-packages.

virtualenv --system-site-packages ENV

Do not use the python interpreter in the virtual environment

If you do not want to or cannot use the python interpreter bin/python in the virtual environment, you need to modify the path of the python library in use.

There is a file activate_this.py in the bin directory:

"""By using execfile(this_file, dict(__file__=this_file)) you willactivate this virtualenv environment.This can be used when you must use an existing Python interpreter, notthe virtualenv bin/python"""

Change path:

activate_this = '/path/to/env/bin/activate_this.py'execfile(activate_this, dict(__file__=activate_this))

Generate a packable Environment

$ virtualenv --relocatable ENV

Python version

-P PYTHON_EXE, -- python = PYTHON_EXE: Specifies the python version of the generated Virtual Environment

virtualenv -p /usr/bin/python2.7 env

Use virtualenv in uwsgi

When deploying a django project, you can use several uwsgi options to specify the virtual environment used by the project. The -- home, -- virtualenv, -- venv, and -- pyhome options can be abbreviated as-H.

Add virtualenv = <path to env> or $ uwsgi-H myenv when running the command in the uwsgi configuration file.

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.