Official documents:
Http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html
Virtualenv is a tool for creating an isolated python environment. VIRTUALENV Create a folder that contains all the necessary executables to use the packages required by the Python project.
It can be used independently, instead of pipenv.
Install virtualenv with PIP:
$ pip install virtualenv
To test your installation
$ virtualenv --version
1. Basic use
Create a virtual environment for a project:
$ cd my_project_folder$ virtualenv my_project
Virtualenv My_project will create a folder in the current directory that contains the Python executable file and a copy of the PIP library so that the other packages can be installed. The name of the virtual environment (in this case, my_project) can be arbitrary, and omitting the name will place the file in the current directory.
In any directory where you run the command, this creates a copy of Python and puts it in a file called My_project.
You can choose to use a Python interpreter (for example python2.7 ):
$ virtualenv -p D/python27/python2.7 my_project
To get started with a virtual environment, it needs to be activated:
$ my_project/Scripts/activate
The name of the current virtual environment will be displayed to the left of the prompt (for example (My_project) your computer: your project username $) to let you know that it is active. From now on, any package you install with PIP will be placed in the My_project folder, isolated from the globally installed Python.
Install packages as usual, such as:
$ pip install requests
If you have temporarily completed your work in a virtual environment, you can deactivate it:
$ my_project\Scripts\deactivate
This will return to the system's default Python interpreter, which will also return to the default, including the installed libraries.
To delete a virtual environment, simply delete its folder. (To do so please perform RM-RF my_project)
Then after a while, you may have many virtual environments scattered throughout the system, and you will probably forget their names or locations.
Virtualenv Common Command Options
virtualenv [OPTIONS] Dest_dir
| - |
- |
| –version |
Displays version information. |
| -h,–help |
Displays help information. |
| -v,–verbose |
Increase the background output information. |
| -q,–quiet |
Control the background output information. |
| -P Python_exe,–python=python_exe |
Specifying the Python Interpreter |
| –clear |
Clears dependent libraries in the virtual environment and initializes the environment. |
| –system-site-packages |
Uses the library that is already installed on the current Python body. |
| –always-copy |
Do not use symbolic links to copy files directly. |
| –no-setuptools |
Don't install Setuptools in the new virtualenv. |
| –no-pip |
Don't install PIP in the new virtualenv. |
| –no-wheel |
Don't install wheel in the new virtualenv. |
2. Other Notes
Running virtualenv with the –no-site-packages option will not include a globally installed package. This can be used to keep the package list clean in case you need to access it later. (This is the default behavior in Virtualenv 1.7 and beyond)
In order to maintain the consistency of your environment, the current state of the "Freeze (freeze)" Environment package is a good idea. To do this, please run:
$ pip freeze > requirements.txt
This will create a requirements.txt file that contains a simple list of all the packages and their respective versions in the current environment. You can use the PIP list to view the list of installed packages without generating a requirements file. This will make it easier for a different developer (or you, if you need to recreate such an environment) to install the same version of the same package later.
$ pip install -r requirements.txt
This helps to ensure consistency between installation, deployment, and developers.
Finally, remember to exclude the virtual Environment folder from source version control and add it to the list of ignore. (view version control ignored)
3. Virtualenvwrapper
Virtualenvwrapper is a virtualenv extension package that manages virtualenv easily. Provides a series of commands that make working with virtual environments much more enjoyable. It puts all your virtual environments in one place.
Install (make sure the virtualenv is installed):
$ pip install virtualenvwrapper-win
Basic use
Create a virtual environment:
$ mkvirtualenv my_project
Switch to a virtual environment
$ workon my_project
Alternatively, you can create a project that creates a virtual environment and creates a project directory in the $WORKON _home. When you use Workon MyProject, you cd-ed to the project directory.
$ mkproject myproject
Virtualenvwrapper provides the tab completion function for the environment name. This is useful when you have many environments and it is difficult to remember their names.
Workon can also stop your current environment, so you can quickly switch between environments.
The stop is the same:
$ deactivate
Delete:
$ rmvirtualenv my_project
Other useful commands
lsvirtualenv
List all the environments.
cdvirtualenv
Navigate to the directory of the currently active virtual environment, for example, so you can browse its site-packages.
cdsitepackages
Similar to the above, but directly into the Site-packages directory.
lssitepackages
Displays the contents of the Site-packages directory.
Using virtual environments under Windows Python