简体   繁体   中英

Reducing requirements.txt

Is there a way to reduce my requirements.txt I switched to Python a year ago and back then I did not completely understand how things work. So when I needed to create requirements.txt I just did a pip freeze and copy passed all the requirements. Today I know that I don't need them all just top-level that import other requirements is there a way to achieve it?

There's a few options.

pip-tools

If you are using pip in your project, you can appreciate pip-tools . First put your requirements manually into requirements.in , and then with pip-compile you can generate the final requirements.txt with all dependencies. Let's say dependencies of your project are Django and Ansible. You put them into requirements.in like this:

# requirements.in
django
ansible

and then run pip-compile to get the whole dependency graph:

$ pip-compile requirements.in

#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile requirements.in
#
ansible==2.9.12           # via -r requirements.in
cffi==1.14.2              # via cryptography
cryptography==3.1         # via ansible
django==1.11.29           # via -r requirements.in
jinja2==2.11.2            # via ansible
markupsafe==1.1.1         # via jinja2
pycparser==2.20           # via cffi
pytz==2020.1              # via django
pyyaml==5.3.1             # via ansible
six==1.15.0               # via cryptography

Poetry

If you are using Poetry , then you can keep adding the dependencies via poetry add , and eventually export them if needed. Otherwise you can skip the export step, as poetry can generate the package for distributing for you via poetry build :

$ poetry add django

Using version ^3.1 for django

Updating dependencies
Resolving dependencies... (1.4s)

Writing lock file


Package operations: 3 installs, 0 updates, 0 removals

  - Installing asgiref (3.2.10)
  - Installing sqlparse (0.3.1)
  - Installing django (3.1)

You can find all dependencies in pyproject.toml . In case you need to export it into requirements.txt :

$ poetry export -f requirements.txt -o requirements.txt --without-hashes

I'm using --without-hashes , since AppEngine has problem with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM