Home >Backend Development >Python Tutorial >Windows installs Virtualenv to create an independent Python environment
Sometimes we need to call different versions of Python packages and modules in our programs, so the virtual environment of Virtualenv can help us isolate them. Next, let’s take a look at installing and using Virtualenv in Windows to create an independent Python environment. Method
0. When will virtualenv be used?
Assume that there are two applications in the system. Among them, application A requires version 1 of the library LibFoo, while application B requires version 2 of the same library LibFoo. The requirements of the two applications for the same library are as follows: There is a conflict, and the libraries in the system are installed to /usr/lib/python2.7/site-packages by default. So how can we use these two applications at the same time?
Your user rights are insufficient to install packages into the site-packages directory
You need to use an application and be independent from other applications in the system.
If you encounter the above problems, then virtualenv will help you. Virtualenv can create an environment with its own installation directory without interfering with the system's shared libraries or other virtualenv environments.
1. Install virtualenv
Before installing virtualenv, we need to install at least one version of Python; because virtualenv is a third-party module of python, it must be installed based on the python environment. ;
If your python environment has pip, then use the command directly:
pip install virtualenv
to install; otherwise you need to download the source code, and then use the command: python install setup.py to install
After installation, you need to create a new independent environment for virtualenv. For details, you can view its help command:
virtualenv -h
Commonly used parameters are:
-p: Specify a version of the python environment; usually used when multiple python versions are installed in your system; by default virtualenv will give priority to its host python environment, That is, which version of Python it is installed under will be selected as the default Python isolation environment by default.
--no-site-packages: Do not use the python installation package of the system environment, that is, the installation package of the real python environment cannot be used in the isolation package; this option is the default in the current version.
--system-site-packages: Contrary to the above, it enables the isolated environment to access the python installation package of the system environment
--distribute: Copy a branch of the python environment. By default, setup, pip, wheel and other basics will be installed. Module
2. Activation
After the installation is complete, you can create an isolation environment. Use the command:
virtualenv envname ##创建一个新的隔离环境 cd envname Scripts\activate ##激活并切换到virtualenv环境
3. After using
to create and activate the virtualenv environment, we can use and install the python installation package and modules in the normal way. You can also directly switch to the directory of the python file that needs to be executed and use python xxx.py to ensure that the python file is executed in an isolated environment.
That is: before we need to use the virtualenv environment, we only need to activate it first and then use the same method to perform python operations and run python files
cd my_project_folder virtual ENV source bin/activate deactivate
The above is the basic usage process of virtualenv. First, virtual ENV will create a virtual environment named ENV in the current directory. At this time, three directories will be automatically generated in the current directory: bin, include and lib. The virtual environment created by default will depend on the site-packages in the system, that is, the libraries already installed in the system can still be used in the virtual environment. If you want to remove the dependency, you can:
virtualenv --no-site-packages ENV
. The virtual environment in the current directory can be started through source bin/activate. After startup, the shell prompt will change, indicating that it has entered the virtual environment. Use deactivate to leave the virtual environment.
4. Integrate into the development environment
Currently, PyCharm supports virtualenv environment integration. Other IDEs have not been studied yet, and integration is also very convenient:
(1) Create a new project
(2) Click the settings icon in the interpreter bar
(3) Select virtualenv
(4) You can create a new one or select an existing virtualenv environment
After configuring, we can also test it in this project, select an installation module that is different from the system environment, and check whether its version is correct. I have 2 django here. version, so I used the django module to test and got the correct results.
For more articles related to installing Virtualenv on Windows to create an independent Python environment, please pay attention to the PHP Chinese website!