question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: pipenv should have a native way to generate requirements.txt directly from the lockfile

See original GitHub issue

When 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:

  1. Run pipenv lock -r. Why this does not work is that it runs pipenv 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.
  2. Run pipenv run pip freeze > requirements.txt (potentially with pipenv 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.
  3. 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
ImreCcommented, Mar 1, 2022

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.

0reactions
matteiuscommented, Apr 6, 2022

This feature has been merged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python HOW: Create requirements.txt Using pipenv
The Pipfile is used to track which dependencies your project needs in case you need to re-install them; Launches a subshell in the...
Read more >
pipenv Documentation - Read the Docs
Pipenv will install the excellent Requests library and create a Pipfile for you ... If you only have a requirements.txt file available when...
Read more >
How to freeze a requirement with pipenv? - Stack Overflow
Pipenv do natively implement freezing requirements.txt. It is as simple as: pipenv lock -r > requirements.txt.
Read more >
Common Pipenv Errors and How to Solve Them: Why Won't it ...
When you begin a project with Pipenv, the tool automatically creates a virtual environment, a Pipfile, and a Pipfile.lock. The Pipfile is very...
Read more >
Dependency Scanning - GitLab Docs
ultimate. The Dependency Scanning feature can automatically find security vulnerabilities in your software dependencies while you're developing and testing your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found