Feature request: pipenv should have a native way to generate requirements.txt directly from the lockfile
See original GitHub issueWhen you just run pipenv install [package] on your project the version in the Pipfile is not pinned, but the lockfile contains the installed package version. Unfortunately not all tooling around Python supports Pipfiles, which means that you very often need to generate a requirements.txt file based on your Pipfile.lock. I think all approaches for this are well described here: https://github.com/pypa/pipenv/issues/3493, but for simplicity I will outline them below:
- Run
pipenv lock -r
. Why this does not work is that it runspipenv lock
, which generates a new lockfile and then generates the requirements. This leads to problematic cases if you want to keep the requirements exactly the same. - Run
pipenv run pip freeze > requirements.txt
(potentially withpipenv sync
before that to make sure that all the packages from Pipfile.lock` are installed). Why this is problematic is because running pip freeze very often leads to too many strict package dependencies being included which leads to dependency mismatches, rendering the whole file unusable. - Some methods based on
jq
or some similar tool that literally parse the lockfile and then output a requirements.txt
I think the problem with option 1 and 2 is that they both do too much. Option 3 is closest to what most people need, but introduces a new dependency on jq
which is also annoying to enforce across a larger group of developers with multiple environments. Which lead to that I am now including a simple python file containing this in almost every repo:
import json
with open('Pipfile.lock', 'r') as f:
reqs = f.read()
with open('requirements.txt', 'w') as f:
f.writelines(f"{req_name}{value['version']}\n" for req_name, value in json.loads(reqs)['default'].items())
I feel that this is so mindbogglingly simple that it should be included somewhere in the pipenv package. My proposition is to include a command pipenv requirements
which reads the Pipfile.lock json file and outputs a requirements.txt based on the default key. Adding --dev
as an option also adds the requirements from the develop key. No locking, no syncing, no pip freezing. Just this.
I am not only asking for this, but also happy to take some time to implement/maintain this if other people support this idea.
Overall, I think pipenv is a vital tool in the Python ecosystem and will continue to use it in any case. Thanks for the work that went into this so far! I think adding this seemingly trivial extra command would resolve some confusion around the requirements.txt (f.e. https://github.com/pypa/pipenv/issues/4731) and therefore would be a relatively low effort high value feature for pipenv.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:12 (5 by maintainers)
Top GitHub Comments
I have a bit of a busy period, but plan to revisit this in a couple of weeks. Also will look into creating a PR at that time. If people are reading this and feel this is a useful addition, please add a thumbs up.
This feature has been merged.