Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --only-explicit Flag to pip freeze to Include Only Explicit Dependencies #13069

Open
1 task done
hasanatkazmi opened this issue Nov 7, 2024 · 2 comments
Open
1 task done
Labels
S: needs triage Issues/PRs that need to be triaged type: feature request Request for a new feature

Comments

@hasanatkazmi
Copy link

What's the problem this feature will solve?

Currently, when using pip freeze -r requirements.txt, the output lists all installed packages, including transitive dependencies. This can clutter the requirements file and make it difficult to maintain or audit, as it includes dependencies that were not explicitly declared by the developer. The current behavior can lead to confusion, unnecessary version locking, and potentially more conflicts when managing dependencies. [example]

Describe the solution you'd like

I am trying to create or maintain a requirements.txt file that only includes the top-level dependencies explicitly declared in the original requirements.txt without any transitive dependencies. This would simplify dependency management, making the file more readable and easier to maintain. Currently, there is no straightforward way in pip to generate this type of output.

I would like to see the addition of a --only-explicit flag to pip freeze. When used with the -r option (e.g., pip freeze -r requirements.txt --only-explicit), this flag would ensure that the output includes only the dependencies explicitly listed in the specified requirements file. This would allow developers to keep their requirements.txt clean and focused on the top-level dependencies they are directly responsible for.

Alternative Solutions

Currently, developers may need to manually curate their requirements.txt to remove transitive dependencies or use external scripts to parse and filter the output of pip freeze. Some developers avoid pip freeze altogether for maintaining requirements.txt due to this issue, which reduces the effectiveness of built-in tools and leads to fragmented workflows.

Additional context

https://medium.com/@tomagee/pip-freeze-requirements-txt-considered-harmful-f0bce66cf895

Code of Conduct

@hasanatkazmi hasanatkazmi added S: needs triage Issues/PRs that need to be triaged type: feature request Request for a new feature labels Nov 7, 2024
@pfmoore
Copy link
Member

pfmoore commented Nov 7, 2024

The information that a package was explicitly requested is not currently recorded in the installation metadata. Before pip could implement something like this, it would need to be standardised as part of that metadata. To do that would need a PEP - if you're interested in proposing and writing such a PEP, feel free to bring the issue up on the Packaging Discourse.

@notatallshaw
Copy link
Member

notatallshaw commented Nov 7, 2024

The general approach to the problem you are describing and the problem the (quite old) blog post you link is to have two files, typically a requirements.in that describes your actual user needed requirements, and a requirements.txt which is a frozen environment that is a solution to the requirements.in at a given moment in time

You can either write your own workflow that manages environments and uses pip freeze to generate this file, or you can use tools that help solve this workflow for you like pip-tools: https://github.com/jazzband/pip-tools?tab=readme-ov-file#pip-tools--pip-compile--pip-sync or the now much faster uv: https://docs.astral.sh/uv/reference/cli/#uv-pip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S: needs triage Issues/PRs that need to be triaged type: feature request Request for a new feature
Projects
None yet
Development

No branches or pull requests

3 participants