1. Virtual environment Overview
Consider the following scenario:
- When we need to maintain a
Django 1.0
Versioning drives an old project and developing one to useDjango 2.0
When 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 installation
Python
And 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 environment
Django
. - Essentially, a virtual environment is a separate folder containing
Python
Interpreters 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: If
Python3/Scripts
Find the path atPython2/Scripts
Before finding the path, will usepython3
Act 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 as
virtualenv -p C:\Python36\python.exe test
3. Virtualenvwrapper is introduced
virtualenv
The 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
virtualenvwrapper
The installation:
*nix
:pip install virtualenvwrapper
windows
:pip install virtualenvwrapper-win
- Note: Install directly
virtualenvwrapper
Can be installed automaticallyvirtualenv
virtualenvwrapper
Basic 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 themkvirtualenv
Default path:
- The default virtual environment is created in:
Computer -- disk C -- User -- User name -- Envs
- in
My computer -> right-click -> Properties -> Advanced System Settings -> Environment variables -> System variables
addWORKON_HOME
, set to the new path.
Specified when creating the virtual environmentPython
Version:
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
,lxml
Etc.), that’s too much trouble. - We can use
pip
The 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 to
requirements
File:pip freeze > requirements.txt
- the
requirements.txt
Move 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:
- use
Pipfile
The file records project dependencies - use
Pipfile.lock
The file records a dependency list for a fixed version
The installationpipenv
pip install pipenv
: Use the default versionPython
Install the virtual environment as an interpreter
Creating a virtual environment
- Go to the project folder
pipenv install
: Use the default versionPython
Install the virtual environment as an interpreterpipenv --three
Use:Python 3
Install the virtual environment as an interpreterpipenv --two
Use:Python 2
Install the virtual environment as an interpreter- The virtual environment will be installed by default on
C:\Users\ your username \.virtualenvs\ folder name - number and letter code
- You can use
pipenv --venv
View the virtual environment path - You can use
pipenv --py
To viewPython
Interpreter path - There will be more when it is installed
Pipfile
和Pipfile.lock
file
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 theta
pipenv
Rather thanpip
- By default, the installation is from the official installation source, which is slow
- Modify the
Pipfile
You can modify the default installation source to increase speed
- I’m going to use theta
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