1. Why use virtual environments:

If you want to develop multiple projects on a single computer, you need to use different versions of the same package. If you install or update in the same directory using the above command, the new version will overwrite the previous version and the other projects will not work.

When using Django to develop projects, one environment can only be used for one project, because it is easy to manage third-party packages. Each project may use a different third party. If you do not install the virtual environment, you will need to load all installation packages every time you load the project, which will affect efficiency. And if you use the same environment, there will be some unexpected errors!

2. Create a Virtual environment in Windows

1.1 installation

pip install virtualenv
Copy the code

1.2 Creating a Virtual Environment

The virtualenv environment name folder will be created and all environments will be placed in the directory D (where you want to put it). The path must not contain ChineseCopy the code
Assumptions: Current computers have python2.7/ PYTHon3.6 virtualenv environment name --python=python3.6 specified version virtualenv environment name --python= "c:\python\python3.6.exe" orCopy the code
PIP install virtualenv 3. Close the terminal and open it again. 4. Run win: >>> D: >>> CD envs 5. Create the virtual environment virtualenv s28Copy the code

1.3 Activating and Exiting a Virtual Environment

Activate: win: >>> CD Scripts Go to the virtual environment Scripts directory >>> > activate Activate the virtual environment (text) G:\Python__all__virtualenv\text\Scripts>Copy the code
Exit: win: >>> CD Scripts Go to the virtual environment Scripts directory >>> deactivate Exit the virtual environment G:\Python__all__virtualenv\text\Scripts>Copy the code

1.4 Installing Modules in a Virtual Environment

  • Activating the virtual Environment

  • Install the module in the active virtual environment

    PIP install django = = 1.11.7 or 1.11.28 PIP install django = = 1.11.7 - http://pypi.douban.com/simple - I trusted host pypi.douban.comCopy the code

2. Create a virtual environment in Linux

2.1 installation

sudo pip install virtualenv
sudo pip install virtualenvwrapper
Copy the code
2. Open the ~/. Bashrc file and add the following: (configuration environment) export WORKON_HOME = $HOME /. Virtualenvs source/usr/local/bin/virtualenvwrapper. Sh # 3, run the source ~ /. BashrcCopy the code

2.2 Creating a Virtual Environment

Create a Python3 environment

mkvirtualenv -p python3 Vrcctv
Copy the code

Create the python2 environment

mkvirtualenv Vrcctv
Copy the code

2.3 Activating and Exiting a Virtual Environment

1. View all virtual environment commands

workon
Copy the code

2. Enter the virtual environment command

Workon Indicates the name of the virtual environmentCopy the code

3. Run the command to exit the virtual environment

deactivate
Copy the code

4. Delete the virtual environment

Rmvirtualenv Virtual environment name Exit first: deactivate then delete: rmVirtualenv VrcctvCopy the code

2.4 Installing Modules in a Virtual Environment

  • Activating the virtual Environment

  • Install the module in the active virtual environment

    PIP install django = = 1.11.7 or 1.11.28 PIP install django = = 1.11.7 - http://pypi.douban.com/simple - I trusted host pypi.douban.comCopy the code