如何在 Python 内检索点要求(冻结)?
How to retrieve pip requirements (freeze) within Python?
我在 git 问题跟踪器上发布了这个问题:https://github.com/pypa/pip/issues/2969
我们能否以某种方式在 python 中调用 pip freeze/list,即不是 shell 上下文?
我希望能够导入 pip 并执行类似 requirements = pip.freeze() 的操作。调用 pip.main(['freeze']) 写入标准输出,而不是 return str 值。
较新版本 (>1.x) 中有 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
输出符合预期:
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
实际上从 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
pip 不支持此处的其他答案:https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program
根据 pip 开发者的说法:
If you're directly importing pip's internals and using them, that isn't a supported usecase.
尝试
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
不建议依赖 pip._internal.operatons
等“私有”函数。您可以改为执行以下操作:
import pkg_resources
env = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
我在 git 问题跟踪器上发布了这个问题:https://github.com/pypa/pip/issues/2969
我们能否以某种方式在 python 中调用 pip freeze/list,即不是 shell 上下文?
我希望能够导入 pip 并执行类似 requirements = pip.freeze() 的操作。调用 pip.main(['freeze']) 写入标准输出,而不是 return str 值。
较新版本 (>1.x) 中有 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
输出符合预期:
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
实际上从 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
pip 不支持此处的其他答案:https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program
根据 pip 开发者的说法:
If you're directly importing pip's internals and using them, that isn't a supported usecase.
尝试
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
不建议依赖 pip._internal.operatons
等“私有”函数。您可以改为执行以下操作:
import pkg_resources
env = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)