【问题标题】:Install using pip a development version of a python package but with stable dependencies使用 pip 安装 python 包的开发版本,但具有稳定的依赖关系
【发布时间】:2018-10-26 04:47:08
【问题描述】:

背景

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

pip install 命令的标志 --pre 告诉 pip 还考虑发布候选版本和开发版本的 Python 包。不过,据我了解,pip install --pre packageA 它将安装 packageA 的开发版本,但也会安装其所有依赖项的开发版本。

问题是:

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

尝试的解决方案

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

我错过了什么?谢谢!

【问题讨论】:

  • 写一个带有固定版本号的requirements.txt。但请确保符合依赖项的条件。
  • 但我不想固定任何软件包的版本...我想要一种可自动化的方式来安装最新版本的packageA(稳定与否,就像--pre 会做的那样)及其依赖项的最新 stable 版本。我不知道如何在 requirements.txt 中应用这些约束,而无需在任何时候手动更改版本号...

标签: python pip


【解决方案1】:

我为此编写了一个脚本(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

此脚本执行以下操作:

# Step 1. get the packages that already installed
pip freeze
# Step 2. install the dev version of packageA
pip install --pre packageA
# Step 3. pick out the dependencies (compare with Step 1)
pip freeze
# Step 4. uninstall all the dependencies of packageA
pip uninstall depend1 depend2 ...
# Step 5. install the stable version of dependencies
pip install depend1 depend2 ...

【讨论】:

  • 但是如果packageA 的开发版本添加了一个新的依赖,就会安装这个依赖的开发版本,对吗?例如,如果 packageA 0.1.1.dev1 添加 numpy 作为依赖项,我希望 packageA 0.1.1.dev1 和最后一个 stable 版本的 numpy 安装。我不想要 numpy 的开发版,只想要 packageA...
  • pip install --pre --no-deps packageA
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 2018-03-04
  • 2019-07-04
  • 2019-10-15
  • 2018-11-30
  • 2011-11-12
  • 2015-08-25
相关资源
最近更新 更多