Create a python virtual environment


Why do we need a virtual environment?
If you are writing a site using Django 1.10.x, then your leaders tell you, before there is an old project is the development of Django 0.9, allows you to maintain, but is no longer compatible 1.10 Django Django 0.9 Some of grammar. This time it will encounter a problem, how do I also have two sets of Django 0.9 Django 1.10 and the environment in my computer it? This time we will be able to solve this problem through the virtual environment.

Installing the Virtual Environment:

virtualenv is used to create a virtual environment software tools, we can pip or pip3 (python3) to install

pip install virtualenv
pip3 install virtualenv

Create a virtual environment:

Create a virtual environment is very simple, the following command can be created:

virtualenv [name of the virtual environment]

Enter the virtual environment:

Created a virtual environment in the future, you can enter into this virtual environment, and then install some third-party packages, enter the virtual environment in different ways in different operating systems, generally divided into two, the first is for Windows, two kinds are * nix:

1. windows into the virtual environment:

  Scripts file into the virtual environment of the folder, and then execute activate.

2. linux into the virtual environment:

  source /path/to/virtualenv/bin/activate

Once you enter into this virtual environment, you install the package, the package is uninstalled in this virtual environment without affecting the outside environment.

Exit the virtual environment:

Exit the virtual environment is very simple, it can be done through a command: deactivate. Create a virtual environment when specified Python interpreter: in the environment variable computer, in general, is not going to change the order of some environment variables. That is like your Python2 / Scripts in front Python3 / Scripts, then you do not often go to change their position. But this time I really want to use Python3 this version, this time can be specified by a particular Python interpreter -p parameter when creating the virtual environment:
 

virtualenv -p C:\Python36\python.exe [virutalenv name]
virtualenvwrapper

virtualenvwrapper This package allows us to manage the virtual environment easier. Do not have to go to create a directory by virtualenv virtual environment, and also went to the activation time to deactivate specific directory.
Installation virtualenvwrapper

linux  :pip install virtualenvwrapper。
windows:pip install virtualenvwrapper-win。

virtualenvwrapper basic use:
1. Create a virtual environment:

mkvirtualenv my_env 

Then creates a Env under your current user folder, and then install the virtual environment to this directory.
If your computer is installed python2 and python3, and both versions are installed virtualenvwrapper, it will use the version of Python environment variable as the first occurrence of this virtual environment of the Python interpreter.
2. Switch to a virtual environment:

workon my_env

3. Exit current virtual environment:

deactivate

4. Delete a virtual environment:

rmvirtualenv my_env

5. List all virtual environments:

lsvirtualenv

Mkvirtualenv modify the default path:
in My Computer -> right click -> Properties -> Advanced System Settings -> Environment Variables -> System Variables add a parameter WORKON_HOME, set the value of this parameter is the path you need.

Create a virtual environment when specified Python version:
When using mkvirtualenv, you can specify --python parameters to specify a particular python path:

mkvirtualenv --python==C:\Python36\python.exe hy_env

Guess you like

Origin www.cnblogs.com/twoo/p/11639351.html