1. Virtual environment Overview

Consider the following scenario:

  • When we need to maintain aDjango 1.0Versioning drives an old project and developing one to useDjango 2.0When you release a new project, it becomes a problem to smoothly switch between two development environments.
  • In order to solve this problem, virtual environment came into being.

What is a virtual environment:

  • First installationPythonAnd then we have oneGlobal level environment(hereinafter referred to asThe global environment), or calledSystem-level environment(hereinafter referred to asSystem environment).
  • We can use the virtual environment tool to create multiple independent virtual environments on the basis of the global environment, which can install different versions of the virtual environmentDjango.
  • Essentially, a virtual environment is a separate folder containingPythonInterpreters and associated dependencies.
  • As shown in the figure below:

Benefits of using a virtual environment:

  • Keep the global environment clean
  • Specify different dependency versions
  • Easy to document and manage dependencies

2. Virtualenv is introduced

Since Python 3.3, a subset of virtualenv has been integrated into Python as part of the Venv standard library. Here’s how to use virtualenv. Those interested in the Venv standard library documentation can learn more about virtualenv.

The installationvirtualenv:

  • pip install virtualenv
  • pip3 install virtualenv

Create a virtual environment:

  • Virtualenv [Name of the virtual environment]

Create a virtual environment in the current folder.

  • Note: IfPython3/ScriptsFind the path atPython2/ScriptsBefore finding the path, will usepython3Act as the interpreter for this virtual environment.

Enter the virtual environment:

  • First go to the virtual environment directory:CD [name of the virtual environment]
  • windows:Scripts\activate
  • *nix:source bin/activate

Exit the virtual environment:

  • deactivate

Specify the interpreter when creating the virtual environment:

  • Virtualenv -p [Python interpreter path] [Name of the virtual environment]
  • Such asvirtualenv -p C:\Python36\python.exe test

3. Virtualenvwrapper is introduced

virtualenvThe shortage of the:

  • The virtual environments created are scattered around and cannot be managed in a unified manner
  • You must enter the virtual environment path to activate the virtual environment, which is troublesome

virtualenvwrapperThe installation:

  • *nix:pip install virtualenvwrapper
  • windows:pip install virtualenvwrapper-win
  • Note: Install directlyvirtualenvwrapperCan be installed automaticallyvirtualenv

virtualenvwrapperBasic use:

  • Create a virtual environment:

Mkvirtualenv Automatically enters the virtual environment after it is created

  • To activate a virtual environment:Workon Virtual environment
  • Exit the virtual environment:deactivate
  • Delete a virtual environment:Rmvirtualenv Virtual environment
  • List all virtual environments:lsvirtualenv
  • Go to the directory where the virtual environment is located:Cdvirtualenv Virtual environment

Modify themkvirtualenvDefault path:

  • The default virtual environment is created in:Computer -- disk C -- User -- User name -- Envs
  • inMy computer -> right-click -> Properties -> Advanced System Settings -> Environment variables -> System variablesaddWORKON_HOME, set to the new path.

Specified when creating the virtual environmentPythonVersion:

  • Mkvirtualenv --python==C:Python36\python.exe Virtual environment name

4. Migrate the virtual environment

Consider the following scenario:

  • Having completed a crawler project in the development environment, you now want to deploy the project to production and have it crawl continuously.
  • If you also manually install the required libraries for the crawler project in the production environment (requests,lxmlEtc.), that’s too much trouble.
  • We can usepipThe package management tool migrates the virtual environment.

Virtual Environment Migration

  • Activate the virtual environment in the development environment
  • Use the package management tool to freeze dependencies torequirementsFile:pip freeze > requirements.txt
  • therequirements.txtMove files to production
  • Activate the newly installed virtual environment in the production environment
  • Install dependencies:pip install -r requirements.txt

5. Pipenv is introduced

Pipenv is another masterpiece by Kenneth Reitz, author of the Requests library. Ambitious as its name indicates, Pipenv combines the capabilities of the traditional PIP package management tool with the Virtualenv virtual environment tool.

Pipenv uses the latest dependency recording standard Pipfile instead of manually passing through requirements.txt. How files record dependencies:

  • usePipfileThe file records project dependencies
  • usePipfile.lockThe file records a dependency list for a fixed version

The installationpipenv

  • pip install pipenv: Use the default versionPythonInstall the virtual environment as an interpreter

Creating a virtual environment

  • Go to the project folder
  • pipenv install: Use the default versionPythonInstall the virtual environment as an interpreter
  • pipenv --threeUse:Python 3Install the virtual environment as an interpreter
  • pipenv --twoUse:Python 2Install the virtual environment as an interpreter
  • The virtual environment will be installed by default onC:\Users\ your username \.virtualenvs\ folder name - number and letter code
  • You can usepipenv --venvView the virtual environment path
  • You can usepipenv --pyTo viewPythonInterpreter path
  • There will be more when it is installedPipfilePipfile.lockfile

Activating the virtual Environment

  • pipenv shell

Install dependencies in the virtual environment

  • Install [package name]
  • Such as:pipenv install flask
  • Note:
    • I’m going to use thetapipenvRather thanpip
    • By default, the installation is from the official installation source, which is slow
    • Modify thePipfileYou can modify the default installation source to increase speed

Do not activate the virtual environment. Run the command in the virtual environment

  • Pipenv the run command

Exiting the Virtual Environment

  • exit

Modify Pipfile to modify pipenv installation source original Pipfile

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.6"

Copy the code

You can change the URL, you can change the name if you want, but it doesn’t really matter. Modified use of tsinghua source Pipfile

[[source]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.6"

Copy the code

Completed in 2019.04.23