When developing Python applications, there is only one version of Python3 installed on the system: 3.4. All third-party packages will be pip installed into the site-packages directory of Python3.
If we want to develop multiple applications at the same time, then these applications will share one Python, which is Python 3 installed on the system. What if application A requires jinja 2.7, and application B requires jinja 2.6?
In this case, each application may need to have its own "independent" Python running environment. virtualenv is used to create an "isolated" Python running environment for an application.
First, we use pip to install virtualenv:
$ pip3 install virtualenv
Then, assuming that we want to develop a new project and need an independent Python running environment, we can do this:
The first step is to create a directory:
Mac:~ michael$ mkdir myproject Mac:~ michael$ cd myproject/ Mac:myproject michael$
The second step is to create an independent Python running environment and name it venv:
Mac:myproject michael$ virtualenv --no-site-packages venv Using base prefix '/usr/local/.../Python.framework/Versions/3.4' New python executable in venv/bin/python3.4 Also creating executable in venv/bin/python Installing setuptools, pip, wheel...done.
Commandvirtualenv can create an independent Python running environment. We also added the parameter --no-site-packages, so that it has been installed into the system Python environment. All third-party packages will not be copied over. In this way, we will get a "clean" Python running environment without any third-party packages.
The newly created Python environment is placed in the venv directory under the current directory. With the Python environment venv, you can use source to enter the environment:
Mac:myproject michael$ source venv/bin/activate (venv)Mac:myproject michael$
Notice that the command prompt has changed. There is a (venv) prefix, indicating that the current environment is a Python environment named venv.
Install various third-party packages normally 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 in the venv environment, and the system Python environment is not affected in any way. In other words, the venv environment is created specifically for the myproject application.
Exit the current venv environment and use the deactivate command:
(venv)Mac:myproject michael$ deactivate Mac:myproject michael$
At this point, we have returned to the normal environment. Now pip or python are executed in the system Python environment.
It is completely possible to create an independent Python running environment for each application, so that the Python environment of each application can be isolated.
How does virtualenv create an "independent" Python running environment? The principle is very simple, that is, copy the system Python to the virtualenv environment, and use the command source venv/bin/activate to enter a virtualenv environment. The virtualenv will modify the relevant environment variables so that the commands python and pip both point to The current virtualenv environment.
summaryVirtualenv provides an isolated Python running environment for applications and solves the problem of multi-version conflicts between different applications.
The above is the detailed content of Python environment setup tutorial using virtualenv. For more information, please follow other related articles on the PHP Chinese website!