This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Software and Hardware Environment

  • Ubuntu 18.04 64 – bit
  • pipenv
  • python3

pipenv

Pipenv is the dependency manager for Python projects. It is an updated version of PIP that can effectively manage complex module dependencies. The project address is github.com/pypa/pipenv.

The basic use

Install using PIP3

pip3 install pipenv --user
Copy the code

This command installs pipenv in the current user’s directory, omits the –user argument, installs it in the system directory and requires sudo permission. After the installation, check the pipenv installation path

Project instance

Let’s create an empty project and see how pipenv works

mkdir pipenv_test
cd pipenv_test
pipenv install
Copy the code

Since this is an empty project, Pipenv will create a new virtual environment for you, generating two special files, Pipfile and pipfile.lock.

Let’s take a look at the virtual environment that Pipenv helped us create, where the Python interpreter resides in your home directory. In addition, for each Python project, pipenv creates virtual environments that are independent of each other, greatly reducing interference between modules and their respective versions.

With the virtual environment created, let’s install a third party module, such as dlib

pipenv install dlib
Copy the code

The command is pipenv uninstall dlib, and the command is pipenv update dlib. After the installation, check the Pipfile. You can see that the Pipfile records the basic information about the installed packages

The pipfile.lock file details the version of the module and the corresponding hash value, which is a bit like the common requirements.txt

The pipenv graph command provides a more detailed view of module dependencies

Install the specified version

Use the dlib module as an example. The default version is 19.15.0. Now you need to specify 19.13.1, which is equivalent to the lower version

Pipenv install dlib = = 19.13.1Copy the code

Then look at the pipfile.lock file

Development vs. production environment

There are usually some Python packages that are only needed in your development environment, such as unit test packages. This is where you need a strict separation between the development and production environments, and PipenV provides just that

pipenv install imutils --dev
Copy the code

If you do not add –dev, you will default to production environment. Look at pipfile.lock after installation

When someone has cloned your project, you can do this if you only need the development environment

pipenv install --dev
Copy the code

Lock the lock

Update the dependencies of the pipfile. lock file to lock the current environment

xugaoxiang@ubuntu:~/pipenv_test$ pipenv lock
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (b3c305)!
Copy the code

Project running

Write a simple script called test.py

import dlib

print(dlib.__version__)
Copy the code

Run the command yes

pipenv run python3 test.py
Copy the code

The resources

  1. github.com/pypa/pipenv