pip-tools = pip-compile + pip-sync
安装
pip-compile
执行pip-compile
编译源文件之setup.py
🔗参考链接: pythonのsetup.pyについてまとめる 🔗参考链接: 编写setup.py
如果目录下存在setup.py,则会自动编译。
以install_requires=['django']为例:
$ pip-compile
#
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
#    pip-compile requirements.in
#
asgiref==3.4.1
    # via django
django==4.0
    # via -r requirements.in
sqlparse==0.4.2
    # via django
编译源文件之requirements.in
执行pip-compile requirements.in后:
$ pip-compile requirements.in
#
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
#    pip-compile requirements.in
#
asgiref==3.4.1
    # via django
django==4.0
    # via -r requirements.in
sqlparse==0.4.2
    # via django
更新编译结果
📢注意:
pip-compile如果检测到requirements.txt已经满足pip-compile的要求,则不会执行任何操作。
如果想要重新生成requirements.txt,需要使用pip-compile --upgrade。
# only update the django package
$ pip-compile --upgrade-package django
# update both the django and requests packages
$ pip-compile --upgrade-package django --upgrade-package requests
# update the django package to the latest, and requests to v2.0.0
$ pip-compile --upgrade-package django --upgrade-package requests==2.0.0
Tips: 可以组合使用--upgrade和--upgrade-package来限制pip-compile只更新指定的包。
使用hash值
$ pip-compile --generate-hashes requirements.in
#
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
#    pip-compile --generate-hashes requirements.in
#
asgiref==3.4.1 \
    --hash=sha256:4ef1ab46b484e3c706329cedeff284a5d40824200638503f5768edb6de7d58e9 \
    --hash=sha256:ffc141aa908e6f175673e7b1b3b7af4fdb0ecb738fc5c8b88f69f055c2415214
    # via django
django==4.0 \
    --hash=sha256:59304646ebc6a77b9b6a59adc67d51ecb03c5e3d63ed1f14c909cdfda84e8010 \
    --hash=sha256:d5a8a14da819a8b9237ee4d8c78dfe056ff6e8a7511987be627192225113ee75
    # via -r requirements.in
sqlparse==0.4.2 \
    --hash=sha256:0c00730c74263a94e5a9919ade150dfc3b19c574389985446148402998287dae \
    --hash=sha256:48719e356bb8b42991bdbb1e8b83223757b93789c00910a616a071910ca4a64d
    # via django
指定输出文件
默认的输出文件的文件名和源文件相同,只是文件后缀由.in变为.txt(如requirements.in变为requirements.txt)。
如果需要指定输出文件,可以使用--output-file选项。
$ pip-compile requirements.in --output-file requirements-django.txt
#
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
#    pip-compile --output-file=requirements-django.txt requirements.in
#
asgiref==3.4.1
    # via django
django==4.0
    # via -r requirements.in
sqlparse==0.4.2
    # via django
可以使用--output-file=-来将输出文件写入标准输出。
pip-compile --output-file=- > requirements.txt
pip-compile - --output-file=- < requirements.in > requirements.txt
使用pip的选项
在使用pip-compile时,可以通过--pip-args选项来指定任意合法的pip选项:
pip-sync
执行pip-sync
$ pip-sync
Collecting asgiref==3.4.1
  Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting django==4.0
  Using cached Django-4.0-py3-none-any.whl (8.0 MB)
Collecting sqlparse==0.4.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.4.1 django-4.0 sqlparse-0.4.2
多个*.txt文件
⭐️如果没有参数为空,那么默认传入requirements.txt.
pip选项
和pip-compile一样,任何合法的pip选项都可以使用--pip-args选项来指定:
注意事项
- 在做版本管理时,应该把
requirements.in和requirements.txt都进行管理