Virtualenv
Virtualenv: Creating a standalone Python virtual environment for a single project
Use of virtualenv:
1. Install virtualenv with the following command
$ sudo pip install virtualenv
2. Create a virtual environment:
- To create a default virtual environment:
$ virtualenv env27
This command indicates the creation of a virtual environment named ENV27. Each virtual environment contains a separate Env27/bin/python and env27/bin/pip that env27 as your running environment when running them.
- To create a virtual environment that specifies the version of the interpreter:
$ virtualenv-p python2.7 env2.7 #解释器为python2.7
-P python3.4 env3.4 #解释器为python3.4
- Create a virtual environment that inherits from a third party
If Python already has third-party libraries installed and you want to use them in a new virtual environment, you can use the following command:
$ virtualenv--system-site-packages env27
Use the following command if you do not want to use:
$ virtualenv--no-site-packages env27
3. Start and exit the virtual environment
- Start the virtual environment
To start the ENV27 virtual environment, simply run the following command:
$ source Env27/bin/activate
This command modifies the system path $path, env27/bin the path to the system path.
The source command represents a change to the current shell environment.
After the virtual environment is started, all new third-party packages for the PIP command will be installed in the current environment without affecting the system environment or other virtual environments. 、
- Exiting a virtual environment
To exit the current virtual environment simply execute the following command:
$ deactivate
Virtualenvwrapper
Virtualenvwrapper is a tool based on virtualenv that manages the desired virtual environment.
Installation
As the following command:
$ sudo pip install Virtualenvwrapper
Virtualenvwrapper all virtual environments are managed by default in the ~/.virtualenvs directory, you can modify the environment variable workon_home to specify the storage directory for the virtual environment.
Use the following command to start Virtualenvwrapper:
$ source/usr/local/bin/virtualenvwrapper.sh
You can also add the command to a shell startup file such as ~/.BASHRC or ~/.profie so that you can use the commands provided by Virtualenvwrapper directly after you log in to the shell.
For virtualenvwrapper configuration:
The code is as follows:
if ' 0 ' ]; Then export Virtualenv_use_distribute=1 # <--all use Pip/distribute Export workon_home= $HOME/.virtualenvs # <--Where all Virtualenvs 'll be stored source/usr/local/bin/virtualenvwrapper.sh export pip_virtualenv_base=$WORKON _home Export pip_respect_virtualenv=Truefi
Add the above configuration to the end of ~/.BASHRC, and then run the following command one time:
SOURCE ~/.BASHRC
Usage
- Create a virtual environment
$ mkvirtualenv env27
- Create a virtual environment for the specified interpreter
$ virtualenvwrapper-p python3.4 env34
- Start the virtual environment
$ Workon env27
- Exiting a virtual environment
$ deactivate
- Deleting a virtual environment
$ rmvirtualenv env27
Use of python------virtualenv&virtualenvwrapper