1. Preface
Because of the numerous versions of Python, there are also Python2 and Python3 debates, so some packages or third-party libraries are prone to version incompatibility issues.
By virtualenv This tool, you can build a series of virtual Python environments, and then install the required packages (in conjunction with PIP) in each environment, which is isolated from each other. As a standalone environment, version issues are not easily available and are also easy to deploy.
2. Installing virtualenv
Install using the following command:
pip install virtualenv
To test whether the installation was successful:
virtualenv --version
Basic use of 3.virtualenv
- Create a virtual environment
Command: VIRTUALENV Virtual environment name
virtualenv env
- Create a virtual environment that specifies the version of the Python interpreter
Command: Virtualenv-p Python interpreter path Virtual environment name
virtualenv -p c:\python27\python.exe env
- Activating a virtual environment
activate venv
- Stop a virtual environment
deactivate
- Deleting a virtual environment
To delete a virtual environment, simply delete its folder.
4. Installing Virtualenvwrapper
Virtualenvwrapper provides a series of commands that make working with virtual environments much more enjoyable. It puts all your virtual environments in one place.
Install under Windows platform using the following command (make sure virtualenv is installed):
pip install virtualenvwrapper-win
5. Set the environment variable Workon_home
Workon_home is a virtual environment created by Virtualenvwrapper-win stored in the path, in Windows, workon_home the default path is C:\Users\username\envs, If we want to customize the path of the store, you can set it by setting the environment variable workon_home.
By computer--Properties--Advanced system settings--environment variables--NEW "variable name" in System variables: workon_home, Variable value: "Your custom Path".
Basic use of 6.virtualenvwrapper
- Create a virtual environment
Command: MKVIRTUALENV Virtual environment name
mkvirtualenv env
- Create a virtual environment that specifies the version of the Python interpreter
Command: Mkvirtualenv-p Python interpreter path Virtual environment name
mkvirtualenv -p c:\python27\python.exe env
Once created, the environment is automatically activated, taking note of changes to the shell prompt:
(venv)c:\>
- List all virtual environments that exist under the Workon_home path
lsvirtualenv
- Activating the virtual environment you need to use
workon env
- Enter the directory of the currently active virtual environment
cdvirtualenv
- Enter the Site-packages directory for the currently active virtual environment
cdsitepackages
- List all packages for the Site-packages directory of the currently active virtual environment
lssitepackages
- Stop a virtual environment
deactivate
- Deleting a virtual environment
rmvirtualenv env
How to install a Python virtual environment under Windows