使用pip安装python包的开发版本,但具有稳定的依赖性

时间:2018-05-16 08:16:32

标签: python pip

背景

pip install命令默认安装python包的最新稳定版本(由PEP426指定的稳定版本)

--pre命令的标志pip install告诉pip还要考虑候选版本和python包的开发版本。据我所知,pip install --pre packageA它将安装packageA的开发版本,但也是其所有依赖项的开发版本。

问题是:

是否可以使用pip来安装软件包的开发版本,但是可以使用其所有依赖项的稳定版本?

尝试解决方案

我尝试过的一件事是安装软件包的稳定版本(具有稳定的依赖项),然后重新安装没有依赖项的开发版本: pip install packageA pip install --pre --no-deps --upgrade --force-reinstall packageA 但问题是,如果packageA的开发版本添加了新的依赖项,则不会安装它。

我什么都错过了?谢谢!

1 个答案:

答案 0 :(得分:0)

我编写了一个脚本(pip_install_dev_and_stable_of_dependencies.py):

#!/usr/bin/env python
import os
import sys


def get_installed_packages():
    with os.popen('pip freeze') as f:
        ss = f.read().strip().split('\n')
    return set([i.split('=')[0].strip().lower() for i in ss])


def install_pre_with_its_dependencies_stable(package):
    already_installed_packages = get_installed_packages()
    os.system('pip install --pre ' + package)
    dependencies = ' '.join([
        p for p in get_installed_packages()
        if p not in already_installed_packages | set([package])
    ])
    os.system('pip uninstall -y ' + dependencies)
    os.system('pip install ' + dependencies)


def main():
    for p in sys.argv[1:]:
        install_pre_with_its_dependencies_stable(p)


if __name__ == '__main__':
    main()

用法:

(venv)$ chmod +x pip_install_dev_and_stable_of_dependencies.py
(venv)$ ./pip_install_dev_and_stable_of_dependencies.py pandas