1 questions
When we have two or even multiple projects A, B, C ..., the Python runtime environment for each project's operational requirements is different. By default, regardless of the project, the global Python environment is used. In this case, the problem is that a, B, C and other projects can only run one; if you run another, you must install the library version that matches it.
2 Solution Ideas
Create a completely separate, local Python environment (or virtual environment). In this environment, install all matching libraries to a project. In short, a project that uses a virtual environment, without interfering with one another.
3 Specific resolution steps
3.1 Installing a third-party package virtualenv
When you have multiple versions of Python in your computer, you can specify a version to install. Code See below:
Pip Install Virtualenv
Or
python3-m pip Install virtualenv
Simple application of 3.2 virtualenv
3.2.1 Creating a local isolated virtual environment
Open a command prompt, and the CD enters the path to the project MyProject (which must be modified according to your own project) to create a virtual environment for the project MyProject.
(1) syntax
VIRTUALENV Virtual Environment Name
Example: Virtualenv ENV
(2) Optional parameters
(i)- p : Specify Python version creation, then use this version of the interpreter, the default is when you install the Viretual package, the use of the Python interpreter
Example: Virtualenv-p C:\Python27\python.exe ENV
(ii) --system-site-packages: A tripartite Library of inherited systems. When the library is retrieved, it will also be found in the system's three-party library, if not added, then only in the current virtual environment to find
Example: Virtualenv--system-site-packages ENV
Once created, an env folder is automatically generated under the MyProject folder, as shown in:
The contents of the Env folder are shown in:
3.2.2 Activating a virtual environment
(1) syntax
Open a command prompt, enter into the virtual Environment Directory/scripts folder, enter Activate.bat
(2) interface effect
(3) effect of effect
In the activation state of the environment, all subsequent operations are based on the installation and uninstallation of this virtual Python environment package. Code execution, such as: Pip install package name (e.g. requests)
If you do not activate the created virtual environment, it will be installed in the global Python environment. When activated, it is installed in a virtual environment.
3.2.3 operation in Active state
Operation of three-party library, execution of PY file, etc.
Note : All of the above actions are in the active virtual environment
3 . 2.4 Exiting the virtual environment
(1) syntax
Go to the Virtual Environment Directory/scripts folder and enter the command Deactivate.bat
(2) interface effect
(3) effect of effect
After exiting, all subsequent actions are again applied to the global Python environment
3.2.5 Deleting a virtual environment
Delete the Env folder in the project file directory directly.
For more commands See official website: https://virtualenv.pypa.io/en/latest/userguide/
4 Supplement
When you give the project to someone later, you can copy it with the virtual environment and the project. But when the virtual environment is very large, this approach is obviously impractical. This requires a different approach.
That is, in a virtual environment, freeze the dependent requirements text and give the project and dependent requirements text to others.
python--Virtual Environment (i)