用于 pip freeze 的 subprocess.popen 不返回包列表

我的代码似乎一直在工作,但现在我只是收到标准输出说,


You are using pip version 9.0.1, however version 18.1 is available.


You should consider upgrading via the 'python -m pip install --upgrade pip' command.

这是我的代码


import subprocess

proc = subprocess.Popen(['pip', 'freeze'], stdout=subprocess.PIPE,

                        stderr=subprocess.STDOUT)


output, err = proc.communicate()

string_file = StringIO.StringIO(output)

print string_file.readlines()

我的目标是获取几个不同包的版本号,并在版本号与我之前在 json 文件中为包的版本记录的版本号不同时执行一些操作。pip list 的子进程似乎也没有帮助。


有谁知道可能导致这种行为的原因,或者是否有更简单的方法来执行此操作?我有大约 10 个包裹要检查。


三国纷争
浏览 172回答 2
2回答

慕勒3428872

显而易见的事情是接受pip的建议并更新它。如果你不能这样做,错误信息应该是无害的——pip执行的检查通常不会阻止它工作。您可以使用禁用版本检查proc = subprocess.Popen(['pip', 'freeze', '--disable-pip-version-check'],                          stdout=subprocess.PIPE,                          stderr=subprocess.STDOUT)

倚天杖

我正在使用 pkg_resources 模块来获取这些信息:import pkg_resourcesfrom subprocess import call# If you want to skip some packagesexception = ['scipy', 'numpy']# List comprehension to get the packagespack_names = [d for d in pkg_resources.working_set    if d.project_name not in exception]# print the package names and versionsfor dist in pack_names:    print('PACKAGE: {}'.format(dist))    # And if you want to upgrade this package:    call('pip install --upgrade --no-cache-dir ' + dist.project_name, shell=True)希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python