How to get write requirements (freeze) in Python?

I posted this question on the Git tracker: https://github.com/pypa/pip/issues/2969

Can we have some way to call pip freeze / list inside python, i.e. not a shell context?

I want to be able to import pip and do something like require = pip.freeze (). The call pip.main (['freeze']) writes to stdout, does not return the value of str.

+9
source share
3 answers

In newer versions (> 1.x) there is pip.operation.freeze:

try:
    from pip._internal.operations import freeze
except ImportError:  # pip < 10.0
    from pip.operations import freeze

x = freeze.freeze()
for p in x:
    print p

The output is as expected:

AMQP == 1.4.6
anyjson == 0.3.3
== 3.3.0.20
defusedxml == 0.4.1
== 1.8.1
-picklefield == 0.3.1
Docutils == 0,12
... .

+21

pip >= 10.0.0 operations.freeze pip._internal.operations.freeze.

, freeze:

try:
    from pip._internal.operations import freeze
except ImportError:
    from pip.operations import freeze
+3

Other answers here are not supported by pip: https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program

According to the developers of the pip:

If you directly import pip internal components and use them, this is not a supported use case.

try

reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
0
source

All Articles