错误:无法打开需求文件:[Errno 2] 没有那个文件或目录:'requirements\.txt'
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements\.txt'
ID: pip/requirements-file-encoding-issue
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| pip 23.3 | active | — | — | — |
| pip 24.0 | active | — | — | — |
| pip 24.2 | active | — | — | — |
根因分析
需求文件路径包含由于 shell 转义或文件系统编码不匹配而被误解的 Unicode 字符(例如文件名中的不间断空格或从右到左标记),导致实际文件路径与指定路径不同。
English
The requirements file path contains a Unicode character that is misinterpreted due to shell escaping or file system encoding mismatch (e.g., a non-breaking space or right-to-left mark in the filename), causing the actual file path to differ from the specified one.
官方文档
https://pip.pypa.io/en/stable/user_guide/#requirements-files解决方案
-
使用 Tab 补全从文件系统中选择精确的文件名:pip install -r ./req<tab>
-
使用 ls -b 列出目录以查看转义字符,并用干净名称重新创建文件:ls -b | grep requirements && cp "$(ls | grep requirements)" requirements_clean.txt
-
使用能显示不可见字符的文本编辑器(例如带 :set list 的 vim)从头重新创建需求文件。
无效尝试
常见但无效的做法:
-
Use a different shell (e.g., bash instead of zsh) to run the pip command
95% 失败
The issue is not shell-specific; the filename on disk contains an invisible Unicode character that persists across shells.
-
Rename the file using mv requirements.txt requirements_fixed.txt
70% 失败
If the filename contains invisible characters, typing 'requirements.txt' in the mv command may also include those characters, resulting in the same filename or a different one.