找不到满足所选需求的版本。

5

我目前尝试安装某些依赖包,但告诉我找不到它们。当我试图注释掉它们时,其他的也出现了这种情况。

我刚刚部署了一个Ubuntu 18.04服务器。通过以下命令创建虚拟环境:python3 -m venv --system-site-packages env但每次运行pip install -r requirements.txt都会失败,并显示:

Collecting apparmor==2.12 (from -r requirements.txt (line 1))
  Could not find a version that satisfies the requirement apparmor==2.12 (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for apparmor==2.12 (from -r requirements.txt (line 1))

如果我尝试安装例如pip install apparmor,它会告诉我

Collecting apparmor
  Could not find a version that satisfies the requirement apparmor (from versions: )
No matching distribution found for apparmor

但是如果我注释掉apparmor,它会告诉我这个:

Collecting apturl==0.5.2 (from -r requirements.txt (line 2))
  Could not find a version that satisfies the requirement apturl==0.5.2 (from -r requirements.txt (line 2)) (from versions: )
No matching distribution found for apturl==0.5.2 (from -r requirements.txt (line 2))

并且问题会在其他人身上随机发生。要求是在我的本地进行的,我的本地也是 ubuntu 18,所以不确定为什么这在本地可以工作,但在新的部署上不能。

我还确保它是最新版本的pip。


1
apparmorapturl无法从PyPI获取,它们是Ubuntu软件包的一部分,只能通过apt install apparmor / apt install apturl安装。你可能已经冻结了系统的站点packages。 - hoefling
@hoefling 这就是问题所在,如果你想把它作为答案。 - nadermx
3个回答

11

apparmorapturl 是 Ubuntu 的软件包,如果你的代码没有使用它们的代码,可以安全地忽略它们;只需从 requirements.txt 中删除它们即可。如果你的代码依赖它们,请通过 apt 确保已安装它们:

apt install -y apparmor apturl && pip install -r requirements.txt

你知道为什么它们被包含在 requirements.txt 中,即使项目并不需要它们吗? - HAL9000
1
@hal9000 如果你是指通过例如 pip freeze 生成 requirements.txt,那么它并不区分代码中实际使用的软件包与未使用的软件包。它只是根据 PEP 376 收集安装的所有内容。最好手动编写要求。 - hoefling

8

当你没有使用虚拟环境来处理Python相关工作时,这会是一个常见的问题。你的requirements.txt列出了你系统或操作系统中所有Python包,而你必须只有你项目所需的包。在某个时刻,你没有使用虚拟环境更新了requirements.txt,并且更新了该文件中的所有Python包,包括你的操作系统和项目所需的包,可能上传到了源代码库。因此,当你想在另一台计算机上运行程序并安装所有包时,你会遇到这种错误...

Python在Ubuntu中是默认安装的,你必须考虑到这一点,也适用于其他系统。

  1. 首要规则是始终使用虚拟环境"虚拟环境文档"
  2. 我知道这很费力,但你可以备份requirements.txt并对其进行清理。然后尝试在没有任何包的情况下运行你的程序(干净的安装),当缺少包时发生错误时,你可以添加它,并使用pip freeze > requirements.txt更新它

2
谢谢,这帮助了我的一天。 - TMKasun

0
解决方案是忽略已经存在且无法通过pypi下载的内置库,如果您需要更改它们,则需要更改Python版本。因此,我使用的解决方法是逐行运行要求文件,以便未找到错误不会停止所有安装过程。这是代码。如果您有任何问题,请随时问我,我是专业人士,可以帮助您。
import subprocess   #Builtin python librarie that will wil used to run commands
with open('requirements.txt') as f: # Opening requirements file
 t=f.read().split("\n") # Spliting the contents of requirements to array of  strings
for e in t: #Creating loop ot iter over each line (element of array)
 subprocess.run("python -m pip install "+e)  #Running the process of python install libraries

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接