Virtualenv is a tool for creating a standalone Python virtual environment that can be created using VIRTUALENV to create a virtual development environment with a separate Python version and an installation library. This allows us to install a variety of required libraries in a virtual environment, so that there is no confusion caused by the excessive use of local libraries. You can also create different versions of Python to accomplish different requirements development.
The corresponding Virtualenvwrapper is an extension in the use of virtualenv.
Windows-virtualenv.
If installed under Windows, you can use PIP to install directly:
install virtualenv
You can manually download the WHL package from PyPI and then go to the corresponding directory using:
pip install virtualenv-15.1.0-py2.py3-none-any.whl
After the installation is complete, you can create the virtual environment in the desired directory:
virtualenv 虚拟环境名称
Enter the virtual directory, run Activate.bat in the scripts directory to enter the virtual environment, and the corresponding use Deactivate.bat exit the virtual environment.
Related steps:
If you want to create a different version of the Python environment, you can add the-p parameter when you create it, followed by the Python.ext full path that corresponds to the path where the Python version is located.
virtualenv -p C:\python2\python.ext py2virtual
After entering virtualenv, you can install any dependent package without affecting the local environment. If you do not want the virtual environment to be able to delete the folder in which it resides, please refer to the official documentation for more information about the VIRTUALENV command
Linux-virtualenv.
Under Ubuntu:
apt-get install python-virtualenv
Under CentOS:
install python-virtualenv
Linux virtualenv installed after the installation can use the Virtualenv py2virtual, the default use of the current system of the Python version, the same can be specified with-P python version of the path to install a different version.
After the installation is complete, go to its directory and use it in the bin directory:
source activate
Can go into the virtual environment, the same use deactivate can exit the virtual environment.
Related actions:
Windows-virtualenvwrapper.
If you use virtualenv, you need to go to the corresponding path, and there are some differences between Linux and Windows, which makes it relatively cumbersome, but you can simplify the operation of the virtual environment by using Virtualwrapper.
installation command:
install virtualenvwrapper-win
Then add workon_home to the environment variable, which is the directory where the generated virtual environment is stored after the command is used.
You can create a virtual environment with simple commands:
mkvirtualenv py3virtualenv
You will create the specified virtual environment and automatically enter it in the directory you have just set up in Workon_home, and use deactivate if you exit.
If you enter again, you don't have to look for the path to virtualenv, you can enter the virtual environment directly using the command:
workon py3virtualenv
Related actions:
You can also specify a python version:
mkvirtualenv --python=python版本的python.exe路径 虚拟环境名称
More commands can refer to the official documentation
Linux-virtualenvwrapper.
Can be used directly in Linux
install virtualenvwrapper
However, after the installation is not directly used, we need to configure before we can use the relevant commands.
Preferred path to find the corresponding virtualenvwrapper.sh file:
find / -name virtualenvwrapper.sh
After locating the path, configure it in/etc/profile:
export WORKON_HOME=/root/virtualenvssource /usr/bin/virtualenvwrapper.sh
Source/etc/profile can then use the relevant commands to manipulate the virtual environment, and the corresponding commands are consistent with Windows.
How to use
All commands are available: virtualenvwrapper --help for viewing, here are a few commonly used:
- To create a basic environment:
mkvirtualenv [环境名]
- To delete an environment:
rmvirtualenv [环境名]
- Activating the Environment:
workon [环境名]
- Exit the Environment:
deactivate
- List all environments:
workon orlsvirtualenv -b
Virtual Environments virtualenv and Virtualenvwrapper (GO)