今天在开发程序,在安装Python的一个依赖时出现了下面的异常:

1
2
3
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory:

HINT: This error might have occurred since this system does not have Windows Long Path support enabled. You can find information on how to enable this at https://pip.pypa.io/warnings/enable-long-paths

从提示中可以看出,这是由于Windows最长路径限制造成的。因为我写的这个程序的目录特别长,而Window10默认对路径的最长限制是260个字符。

修改的方式我们只要参考这里即可:Maximum Path Length Limitation - Win32 apps | Microsoft Learn

文章一开头就告诉我们:

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is “D:_some 256-character path string_<NUL>“ where “<NUL>“ represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

翻译过来就是

[!note]
在Windows API中(下文段落会讨论一些例外情况),路径的最大长度为MAX_PATH,其定义为260个字符。本地路径的结构顺序如下:驱动器号、冒号、反斜杠、由反斜杠分隔的名称组件,以及一个终止空字符。例如,D盘上的最大路径为“D:\some 256-character path string<NUL>”,其中“<NUL>”代表当前系统代码页中不可见的终止空字符。(此处使用< >是为了视觉上的清晰,它们不能成为有效路径字符串的一部分。)

解决的办法文中也告诉我们了,只要用Windows管理员权限打开Powershell,然后输入下面的命令即可

1
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force