Search code examples
pythoninstallationpip

how can I generate a python environments's minimum requirements?


I keep coming up with different ways to do this and it ends up being a PITA no matter how I do it and I cannot be the only one trying to deal with this.

Say that I do this:

virtualenv .venv
alias tp='./.venv/bin/python3'
tp -m pip install AAA
tp -m pip install BBB
tp -m pip install CCC
tp -m pip freeze

Installing each of AAA, BBB, and CCC installed a bunch of other things. The freeze result ends up looking like:

5D68A4E
AAA
A864534
BBB
EE715F8
F9556BC
CCC
G5B409B
K4F6047

What I want to know is this. I want to have my requirements.txt contain only AAA, BBB, and CCC. But I do not want to have to do some separate thing every time I do an install to have this happen.

Is there a way to get freeze to give me the "primary" installs and not the other ones that we installed as a side-effect? It would be good to be able to generate this after the fact. Is it possible?


Solution

  • Pip does not have this option, it doesn't keep track of wether a package was installed explicitly or as a dependency.

    You either have to work this our manually or use other tools.

    For example:

    I know that conda is able to do this with conda export --from-history - I don't think it can correctly track pip installed dependencies though

    There are tools like https://pypi.org/project/pip-chill/ that, although not exactly what you are looking for, try to minimize the size of requirements.txt to a minimum number of packages. I have not used it myself, so no idea how well it works