Information reference: Liaoche's official website https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001432712108300322c61f256c74803b43bfd65c6f8d0d0000
I. BACKGROUND
If the system has only one version of Python3 installed--3.4, if we want to develop multiple applications at the same time, then these applications will share a python, is installed in the system of Python 3, all third-party packages will be pip
installed into the Python3 site-packages
Directory. 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.
Second, virtualenv installation
$ PIP3 Install virtualenv
Iii. What should I do when I need a separate running Python environment?
1. Create a Directory
mac:~ michael$ mkdir myprojectmac:~ michael$ cd myproject/mac:myproject michael$
2. Create a stand-alone python environment namedvenv
Mac:myproject michael$ virtualenv--no-site-packages venvusing base prefix '/usr/local/.../python.framework/versions/ 3.4 ' New python executable in venv/bin/python3.4also creating executable in Venv/bin/pythoninstalling Setuptools, Pip, whe El ... 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/activate (venv) Mac:myproject michael$
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 jinja2 ... Successfully installed JINJA2-2.7. 3 markupsafe-0.23(venv) mac:myproject michael$ python myapp.py ...
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.
3. Exit the current VENV environment
(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.
Iv. Summary
It is entirely possible to create a separate Python runtime environment for each app, so that each app's Python environment can be isolated.
Virtualenv is the principle of creating a "stand-alone" Python environment: Copy the System python to a virtualenv environment, and source venv/bin/activate
when you enter a virtualenv environment with a command, virtualenv modifies the relevant environment variables, allowing the command python
and pip
both point to the current virtualenv environment.
VIRTUALENV provides an isolated Python runtime environment for applications that resolves multiple versions of conflicts across different applications.
Python Learning Note 02:python virtual environment