在github下载了一份代码,在服务器上执行pip install -r requirements.txt
时报如下错误:
ERROR: Could not find a version that satisfies the requirement torch==2.1.2 (from versions: 1.0.0, 1.0.1, 1.0.1.post2, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.8.1, 1.9.0, 1.9.1, 1.10.0, 1.10.1, 1.10.2)
ERROR: No matching distribution found for torch==2.1.2
- 在服务器上,我使用
conda create --name envs_name python==3.11.7
命令创建了一个新的环境,然而当我执行pip install -r requirements.txt
命令时却报以上错误。 - 于是通过以上报错信息在网上寻找答案,然而基本上都是让我尝试修改pip源,或者降低版本安装。这并没有解决我的问题。
- 后面我继续尝试使用
pip install package
命令来安装包,直到有一次报错信息的最下面给出了一条信息:
Requirement already satisfied: pip in /home/jsjx/.local/lib/python3.6/site-packages (21.3.1)
很敏感的看到python3.6与conda环境中的python3.11.7版本不一致,并且pip的版本为21.3.1。于是我尝试使用pip install --upgrade pip
升级pip的版本。命令行的结果显示:
/usr/lib/python3/dist-packages/secretstorage/dhcrypto.py:15: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
from cryptography.utils import int_from_bytes
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pip in /home/jsjx/.local/lib/python3.6/site-packages (21.3.1)
看到这里,我恍然大悟。在我使用pip执行命令时,系统调用的pip指令并不是conda环境中的,而是系统上默认的3.6版本。那我们该如何使用conda环境中的pip呢?
- 使用绝对路径:在 conda 环境中,使用绝对路径来调用指定 conda 环境中的 pip。
- 重新配置环境变量:检查你的环境变量 PATH,确保 conda 的路径在系统的 PATH 变量中优先级较高。你可以编辑 .bashrc 或 .bash_profile 文件。
在这里,我选用了第一种方法。通过调用/anaconda3/envs/your_env/bin/pip install package_name
成功安装了所需的包。