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.