在离线环境中安装 Python 包未找到已安装的依赖项

Installing Python package in offline environment does not find installed dependency

我正在尝试在离线环境中从源代码安装 SQLACodegen 包及其依赖项(inflect、setuptools-scm)。具体来说,我在 Red Hat Enterprise Linux 7 系统上使用 Python 3.8 安装了 Anaconda 2020.07。

我在 PATH 前面添加了 Anaconda bin 目录,我使用 python -m pip install whatever.tar.gz 安装。是的,我知道像这样使用 pip 而不是 conda 是一种罪过。碰巧的是,这是安装上游软件包时更简单的方法,而且我之前在安装许多其他软件包时从未遇到过问题。

问题是 pip 正在尝试上网下载和安装 setuptools。奇怪的是setuptools已经安装好了,并且符合包的版本要求。即使我指定了诸如 --no-index -f /path/to/packages 之类的 pip 标志,它仍然无法检测到已安装的 setuptools 并尝试从某个地方提取它。

示例输出:

[root@myserver stuff]# ls
inflect-4.1.0.tar.gz  setuptools_scm-4.1.2.tar.gz  sqlacodegen-2.1.0.tar.gz
[root@myserver stuff]# which python
/usr/local/anaconda-2020.07/bin/python
[root@myserver stuff]# python -V
Python 3.8.3
[root@myserver stuff]# python -m pip list | grep setuptools
setuptools                             49.2.0.post20200714
[root@myserver stuff]# python -m pip install --no-index -f . sqlacodegen-2.1.0.tar.gz
Looking in links: .
Processing ./sqlacodegen-2.1.0.tar.gz
  Installing build dependencies ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/local/anaconda-2020.07/bin/python /usr/local/anaconda-2020.07/lib/python3.8/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env/r61p50oe/overlay --no-warn-script-location --no-binary :none: --only-binary :none: --no-index --find-links . -- 'setuptools >= 36.2.7' wheel 'setuptools_scm >= 1.7.0'
       cwd: None
  Complete output (3 lines):
  Looking in links: .
  ERROR: Could not find a version that satisfies the requirement setuptools>=36.2.7 (from versions: none)
  ERROR: No matching distribution found for setuptools>=36.2.7

我觉得 --ignore-installed 标志可能是问题的一部分,但我不知道如何覆盖它,它看起来像 pip 对许多选项的影响。有没有人遇到类似情况并解决了?

你需要在从源安装时添加 install 选项 --no-build-isolation 并且你想告诉 Pip 使用已经安装的包来满足构建依赖性。强调建立依赖关系。

根据 PEP 518,最常见的构建依赖项是 setuptoolswheel。如此之多,以至于作为“构建系统执行的最低要求”,构建工具应该默认将它们添加到构建配置中。

从源安装新包时,就构建依赖性而言,Python 环境中的包将被忽略。作为 Pip documentation explains:

When making build requirements available, pip does so in an isolated environment. That is, pip does not install those requirements into the user’s site-packages, but rather installs them in a temporary directory which it adds to the user’s sys.path for the duration of the build. This ensures that build requirements are handled independently of the user’s runtime environment. For example, a project that needs a recent version of setuptools to build can still be installed, even if the user has an older version installed (and without silently replacing that version).

(此文档已更新和改写,请参阅第 Build Process 部分,并且不再明确提及 site-packages 将被忽略,尽管这仍然是隐含的。)

在您的示例中,该临时目录似乎是 /tmp/pip-build-env/r61p50oe。除了 --no-build-isolation 选项,您还可以将 setuptoolswheel 的源包(可能更多,具体取决于您正在安装的包)添加到通过 -f/--find-links选项。