When developing a Python project, first decide which version of Python to use. Currently, the default is 3.7 (Python 2.7 will be discontinued in 2020, but there will be many projects based on Python 2.x to maintain). Second, a specific version of a third-party library is selected based on the project’s requirements (the latest version is usually selected, unless there is a conflict between different libraries). However, third-party libraries installed using PIP are installed in Python3’s site-packages directory by default, making it difficult to deal with conflicts between third-party library versions in different projects. Therefore, we need to create separate clean space for development and deployment of different projects. This is where the Virtual environment of Python is needed. This article describes both Virtualenv and Pipenv approaches to building virtual environments, but pipenv’s approach is worth promoting.

virtualenv

Virtualenv is a tool for creating isolated Python environments. It creates a folder containing all the necessary executables to use the packages required by the Python project. In other words, we can use Virtualenv to create Python environments for each project, and install packages that are independent of each other. Use Avtive’s corresponding Python environment by activating its corresponding Python environment. To facilitate the management of different Python environments, we use virtualenvWrapper to manage the Python virtual environment.

Installation and configuration

Virtualenv and VirtualenVWrapper are installed with a simple command:

pip install virtualenv  # to install virtualenv
pip install virtualenvwrapper # installation virtualenvwrapper
Copy the code

You do not need to configure virtualenv. To use virtualenv, run the virtualenv proEnv command in the project folder to generate a proEnv directory in the current project directory. The directory contains bin, include, lib, Local and a pip-selfcheck.json file. When the virtual environment is activated source proEnv/bin/activate, all PIP installers executed will be installed in the current virtual environment folder proEnv. But this leads to the following problem:

  1. Inconvenient to manage – active, inactivedeactivate, environment switching, etc
  2. Not reusable — stored in the specified project directory

Virtualenvwrapper solves these problems perfectly.

The configuration of VirtualenVWrapper is as follows:

# 1. At the end of the ~/.bashrc file add the following (depending on the specific environment)
source /usr/local/bin/virtualenvwrapper.sh

# 2. Reload the configuration
source $HOME/.bashrc
Copy the code

Common commands

# Create environmentMkvirtualenv proEnv [-p python3.7]# Create project Python environment in $HOME/. Virtualenvs/directory

# List all environments
lsvirtualenv

# Switch environment
workon proEnv

# Exit environment
deactivate

# Delete environment
revirtualenv proEnv
Copy the code

pipenv

Pipenv is a command-line utility written by Kenneth Reitz, the chief proponent of Pipfile and author of requests, that includes Pipfile, PIP, click, requests, and virtualenv. Manage multiple Python environments, third-party packages and modules.

Problems solved by Pipenv:

  • Different projects rely on different third-party libraries and package versions (Virtualenv can also solve this problem)
  • (For example, autopEP8, PyLint, PEP8, etc.)

Pipenv features:

  • Pipenv integrates the functionality of both PIP and Virtualenv, while fixing some of the drawbacks of both.
  • In the past, managing the requirements. TXT file with Virtualenv might have been problematic. Pipenv uses Pipfile and pipfile. lock, the latter of which holds the dependencies of packages.
  • Hash validation is used everywhere, both installing and uninstalling packages are secure, and security holes are automatically exposed.
  • Simplify the development workflow by loading the.env file.
  • Python2 and Python3 are supported, and the commands are the same across platforms.

TIPS: The authors of Flask Web Development use Pipenv as a package management tool in their book, but in August 2019, they posted a blog about not using Pipenv. The core point is that Pipenv updates all the packages it relies on when updating third-party dependency libraries. The reason for this is that the corresponding packages use an asterisk * and no specific package version is specified. The update/sync operation will update all third-party libraries whose versions are not specified (with asterisks *). It is strongly recommended that all third-party libraries in the production environment be versioned.

The pipfile. lock file is more like a snapshot of the current environment, and does not mean that the version of the third-party dependencies you installed is fixed, unless you qualify the version in the Pipfile.

The installation

The installation of Pipenv is simple with a single command:

Install the Pipenv package to manage dependency libraries
python3 -m pip install pipenv
Set the environment variable and create the virtual environment.venv in the project directory
export PIPENV_VENV_IN_PROJECT=1
Copy the code

A common Pipfile configuration is as follows:

[[source]]
name = "aliyun"
url = "https://mirrors.aliyun.com/pypi/simple"
verify_ssl = true

[dev-packages]
autopep8 = "*"
pylint = "*"
pep8 = "*"


[packages]
flask = "= = 1.1.2"

[requires]
python_version = "3.7"
Copy the code

Common command

  • Creating a virtual environment
The development environment installs third-party dependencies in Pipfile
pipenv intall -d
# Generate environment to install third-party dependencies in Pipfile
pipenv install
Install dependencies in requirements.txt (project migration)
pipenv install -r requirements.txt
# Create requirements.txt from pipenv dependencies (project migration)
pipenv run pip freeze > requirements.txt
Copy the code
  • Using the environment
# Run directly in the virtual environment
pipenv run python test.py
Activate the virtual environment and run the script
pipenv shell && python test.py && exit
Copy the code
  • Install and uninstall third-party dependencies
Install third-party dependency libraries and add them to the development environment of Pipfile
pipenv install -d pylint
Install the specified version of third-party dependencies and add them to the Pipfile build environmentPipenv install Flask = = 1.1.2# Uninstall third-party dependencies and update Pipfile
pipenv uninstall Flask
Copy the code
  • Viewing dependency Packages
Check the packages that the current environment depends on
pipenv graph
Copy the code

How virtual environments work

Pipenv is based on Pipfile, combined with PIP and Virtualenv to help developers manage dependencies. To understand how virtual environments work, you only need to understand virtualenv. Here we experimented with the Pytyhon: 3.7-Stretch image (not the Alpine image because its default shell is Ash, not bash).

The phenomenon of analysis

There are two characteristics of virtual environment:

  • The Python version is fixed. Even if the system’s Python is upgraded, the virtual environment is still unaffected and development status remains.
  • All Python packages are valid only in this environment. Once you exit, you return to the default environment for the user/system.

So it is assumed that virtualenv is implemented by changing the shell’s PATH to the specific Python version, and that the corresponding packages are maintained in their respective virtual environment directories.

Validation means

  • echo $PATHSearch path for the Python command
  • pip listView the dependent libraries for the current environment
  • python -m sitePrints debugging information about the current Python environment and site-packages

validation

# Start containerDocker run-it --rm Python :3.7-stretch bashInstall virtualenv and VirtualEnVWrapper
pip install virtualenv virtualenvwrapper
# in $HOME/bashrc file add the source/usr/local/bin/virtualenvwrapper. Sh
echo "source /usr/local/bin/virtualenvwrapper.sh" >> $HOME/.bashrc
# Reload the configuration
source $HOME/.bashrc
# Create virtual environment proEnv
mkvirtualenv proEnv
Copy the code

The current environment of the container is as follows:useworkon proEnvAfter entering the virtual environment proEnv, the parameters are shown as follows:

Virtualenv puts the bin directory of the virtual environment first in the PATH, so when python is executed on a terminal, Perform/root /. Virtualenvs proEnv/bin/python, instead of/usr/local/bin/python. This is why Virtualenv does not affect the local Python environment.

Compare the results of Python-m site, Found that virtual environments sys. Third party library directories in the path consists of/usr/local/lib/python3.7 / site – packages changed / root/virtualenvs proEnv/lib/python3.7 / site – packages. This is why PIP List does not see packages and is the biggest secret of environmental isolation.

reference

  1. Pipenv Chinese document
  2. Python Best Practices Guide
  3. Still using Virtualenv for Python development? Let’s get to know Pipenv
  4. Don’t use Pipenv

If this article has helped you, or you are interested in technical articles, you can pay attention to the wechat public number: Technical tea Party, can receive the relevant technical articles in the first time, thank you!

This article was automatically published by ArtiPub, an article publishing platform