How to retrieve pip requirements (freeze) within Python?
The other answers here are unsupported by pip: https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program
According to pip developers:
If you're directly importing pip's internals and using them, that isn't a supported usecase.
try
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
It's not recommended to rely on a "private" function such as pip._internal.operatons
. You can do the following instead:
import pkg_resources
env = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
There's a pip.operation.freeze in newer releases (>1.x):
try: from pip._internal.operations import freeze
except ImportError: # pip < 10.0
from pip.operations import freeze
pkgs = freeze.freeze()
for pkg in pkgs: print(pkg)
Output is, as expected:
amqp==1.4.6
anyjson==0.3.3
billiard==3.3.0.20
defusedxml==0.4.1
Django==1.8.1
django-picklefield==0.3.1
docutils==0.12
... etc