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.
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
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
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.
Recommended Posts