我正在 Ubuntu 22.04 LTS 系统上使用官方文档设置 pip,当我执行
python get-pip.py
命令时,我收到一条错误消息:
python3: can't open file '/home/usr/get-pip.py': [Errno 2] No such file or directory
我认为这是我的错误,因为
python
命令在 Linux 发行版中不起作用,并且我给 python3 命令起了别名:
alias python='python3'
问题依然存在。快速的谷歌搜索让我找到了这个解决方案,我的错误得到了解决。
后来,我深入研究了 [Errno 2] 是什么,但我遇到的所有材料都过度关注解决此错误,而不是导致它的原因。
errno
中,具体来说:
errno.ENOENT
。FileNotFoundError
os.strerror()
或errno.errorcode
:
>>> import os
>>> os.strerror(2)
'No such file or directory'
>>>
>>> import errno
>>> errno.errorcode[2]
'ENOENT'
标准 Linux 错误代码可以通过运行
man 3 errno
或参考 errno(3) — Linux 手册页找到。
ENOENT No such file or directory (POSIX.1-2001).
Typically, this error results when a specified pathname does not exist, or one of
the components in the directory prefix of a pathname does not exist, or the speci‐
fied pathname is a dangling symbolic link.
它的意思就是:该文件不存在。
Python 命令 do 在 Linux 上工作,无需在此处使用别名
python
。你运行了 python get-pip.py
并且 python 告诉你它找不到 get-pip.py
。您从当前工作目录运行/home/usr/
,但 python 试图友好地显示不存在文件的绝对路径。
说明告诉您下载 get-pip.py,切换到下载目录,然后运行命令。您一定错过了其中一个步骤。
适合您的解决方案将 get-pip.py 下载到当前工作目录并执行它。这就是它起作用的原因。首先
sudo
可能不太明智,因为与安装本地 python 相比,您可能会面临覆盖平台安装的 python 的风险。