Problem Description
In software development, it is often necessary to run environments of different versions and install modules of different versions. For example, some projects run in django 2.0, and some projects run in django 3.1.
This is a common requirement, and many programming languages need to face this problem. However, for various reasons (conflict, incompatibility), these modules cannot be installed at the same time, what should I do?
This note will record: How to run multiple different versions and independent Python environments locally to solve the problem of multiple projects requiring different versions of Python environments.
solution
Solution 1: Virtual Environments
In Python, Virtual Environments is the solution to this problem. After the virtual environment is created, related operations will occur in that environment. Here "related operations" refer to operations related to the Python environment. For example, pip install will install the module into the virtual environment without affecting the system environment.
Solution 1: The establishment and use of virtual environment
Build a virtual and independent python runtime environment to make the runtime environment of a single project independent of other projects.
The steps to build a virtual environment are as follows:
### The first step, install the virtualenv module pip3 install virtualenv ### The second step, create a virtual environment # Executing this command will create the ./venv-example directory virtualenv --python=python3 "venv-example" ### The third step, activate the virtual environment # After executing this command, we will enter the virtual environment source venv-example/bin/activate ### Fourth step, execution operation # All operations related to the Python environment will occur in this environment, That is to say: Module installation will be installed in the # corresponding directory under venv-example , and these modules will be loaded from the corresponding directory when in use.
Virtual environment management
You can use virtualenvwrapper to manage virtual environments.
Related Links
references
WikiNotes/Run multiple different versions at the same time: virtual environment (study notes)
The use of virtual environment in
Python Python virtual environment
Python virtual environment virtualenv