[PYTHON] Use of constraints file added in pip 7.1

Introduction

Some features have been added to pip, which is used when installing Python libraries, since version 7.1.

Its feature, called Constraints File, is not very well known, but it can be very useful in some ways.

Features of Constraints File

The functionality of Constraints File and Requirements File is very similar, and their formats are exactly the same.

The only difference is that Constraints File only controls the version and does not install the libraries you write.

For example, when there is the following file

werkzeug.txt


werkzeug=== 0.11.1

Werkzeug 0.11.1 is installed when used as a Requirements File as follows:

pip install -r werkzeug.txt  # 0.11.1 is installed

On the other hand, werkzeug is not installed even if it is set as Constraints File.

pip install -c werkzeug.txt #Nothing is installed

However, you can control the installed version by using it together with the argument as follows.

pip install werkzeug -c werkzeug # 0.11.1 is installed

You can also use Requirements File and Constraints File together.

In summary, it is as follows.

$ pip install -r werkzeug.txt #The specified version will be installed
$ pip install -c werkzeug.txt #Nothing is installed
$ pip install -r werkzeug.txt -c werkzeug.txt #The specified version will be installed
$ pip install werkzeug -c werkzeug.txt #The specified version will be installed

Convenient use of Constraints File

Constraints File works best when combined with Requirements File.

A common use of Requirements File was to write the output of pip freeze as is. However, this method cannot distinguish between the installed library and the dependent libraries of that library.

For example, suppose a library named A depends on libraries named B and C. At that time, if you pip freeze after installing A, B and C will be output, and there is no way to distinguish them.

$ pip install A
$ pip freeze
A==1.0.0
B==1.0.0
C==1.0.0

Here, by describing only the directly dependent libraries in the Requirements File and describing the result of pip freeze in the Constraints File, it is possible to distinguish between the directly dependent libraries and the indirectly dependent libraries. Can be done.

$ cat requirements.txt
A
$ cat constraints.txt
A==1.0.0
B==1.0.0
C==1.0.0
$ pip install -r requirements.txt -c constraints.txt

Summary

In this article, I introduced you to Constraints File.

It's still a new feature, so you may be able to use it more conveniently than the ones introduced here. If you find such a convenient way to use it, I would be grateful if you could let me know.

reference

Recommended Posts

Use of constraints file added in pip 7.1
Use jinja2 template in excel file
The story of the "hole" in the file
Efficient use of Linux file system
Handling of character code of file in IronPython
R: Use Japanese instead of Japanese in scripts
Use the latest pip in a virtualenv environment
Summary of how to use MNIST in Python
Date of Address already in use error in Flask
Speed evaluation of CSV file output in Python
How to use template engine in pyramid 1 file application
Let's use the open data of "Mamebus" in Python
Use date to x-axis of tsplot depicted in seaborn
Convenient use of ipython
File operations in Python
File processing in Python
Use DataFrame in Java
Use dates in Python
Use Mean in DataFrame
Use Valgrind in Python
Use pip with MSYS2
Graceful use of MySQLdb
File operations in Python
Use ujson in requests
Put pip in Blender
Use profiler in Python
Reimport of .py file
Use pip on Windows
Survey on the use of machine learning in real services
[Note] Import of a file in the parent directory in Python
Comparison of how to use higher-order functions in Python 2 and 3
Install packages that cannot use pip in Azure App Service
Google search for the last line of the file in Python
Command to list all files in order of file name