我想弄清楚如何使用pip作为一个模块。具体来说,我希望能够查询本地pypi服务器的可用模块版本号。
例如,我已经了解到,我可以这样做来获得安装在我的机器上的软件包列表:
import pip
for dist in pip.get_installed_distributions():
    print dist.key, dist.version我想要等价物,但用于在我自己的pypi服务器上获得可用的包。是否有一种很好的方法来做到这一点,或者pip不是真正被设计为除了pip命令行实用工具之外的任何其他用途的?
最终,我要完成的是为我正在编写的程序构建自动更新功能,所以我需要能够获得我所拥有的版本和可用的版本。
我正在寻找python2.7的解决方案。
发布于 2013-09-19 19:20:51
您可以使用命令行pip list -o到列出过时的软件包。
如果您想将它作为一个模块使用,您必须复制pip正在做的事情,因为它期望从命令行中使用。以下函数将输出一个元组列表(“包”、“当前版本”、“最新版本”),前提是您只想查看本地服务器
from StringIO import StringIO
import sys
import re
from pip import parseopts
from pip.commands import commands
def list_outdated(pypi_server):
    args = ['list', '-o', '-f', pypi_server, '--no-index']
    cmd_name, options, args, parser = parseopts(args)
    command = commands['list'](parser)
    _stdout = sys.stdout
    output = StringIO()
    sys.stdout = output
    command.main(args, options)
    sys.stdout = _stdout
    return re.findall('(\w+)\s+\(Current:\s+(.*?) Latest:\s+(.*?)\)', output.read() * 2)
outdated = list_outdated('http://my_server:8080/packages/')https://stackoverflow.com/questions/18881583
复制相似问题