How to retrieve pip requirements (freeze) within P

2019-04-07 22:44发布

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

Can we have some manner of calling pip freeze/list within python, i.e. not a shell context?

I want to be able to import pip and do something like requirements = pip.freeze(). Calling pip.main(['freeze']) writes to stdout, doesn't return str values.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-07 23:20

Actually from pip >= 10.0.0 package operations.freeze has moved to pip._internal.operations.freeze.

So the safe way to import freeze is:

try:
    from pip._internal.operations import freeze
except ImportError:
    from pip.operations import freeze
查看更多
趁早两清
3楼-- · 2019-04-07 23:22

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

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

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

查看更多
登录 后发表回答