问题背景
现如今,在应用商店中已经可以很方便地安装python了,而在使用过程中,意外发现这种安装会带来超长路径,从而影响使用。
案例
使用pip安装pytorch过程中,出现了以下报错:
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:'C:\\Users\\UserName\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\\LocalCache\\localpackages\\Python37\\sitepackages\\caffe2\\python\\serialized_test\\data\\operator_test\\collect_and_distribute_fpn_rpn_proposals_op_test.test_collect_and_dist.zip
出错原因是Windows默认没有开启超长路径的支持,而应用商店安装的应用出现了超长路径,因此需要开启超长路径支持。
解决方案
开启超长路径支持,在注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem路径下,LongPathsEnabled键值中,修改键值为1,开启支持即可。
参考文献
I got here by having this kind of error when I tried installing
tensorflowlibrary. My error was the following:Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'c:\moj ifajlovi\faks\11master\1semestar\siap-sistemizaistrazivanjeianalizupodataka_(datamining)\projek at\rad\venvs\siap_venv\Lib\site-packages\tensorflow_estimator\python\estimator\canned\line ar_optimizer\python\utils\__pycache__\sharded_mutable_dense_hashtable.cpython-37.pyc'
So, there was no
sharded_mutable_dense_hashtable.cpython-37.pycfile in the__pycache__directory. But,sharded_mutable_dense_hashtable.cpython-37.pycfile was in theutilsdirectory (which is the parent directory of__pycache__directory).That's why I tried manually copying the
sharded_mutable_dense_hashtable.cpython-37.pycfile in the__pycache__directory. When I tried that, I had a copy error which stated that the path was too long, so it couldn't put the file in the directory.So, the solution:
Install the desired python package (in my case tensorflow) in the folder which has a shorter path (for example
C:/my_py_packages/some_package) or set the registry valueHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabledto1as mentioned here.
The same error message can bite you when you pip install pytorch after getting python from Microsoft Store. +1 since your post got me onto the root cause "path too long", see reddit.com/r/pytorch/comments/c6cllq/issue_installing_pytorch – Jonas Heidelberg Aug 5 at 22:05
https://docs.python.org/3.7/using/windows.html
3.1.2. Removing the MAX_PATH Limitation¶
Windows historically has limited path lengths to 260 characters. This meant that paths longer than this would not resolve and errors would result.
In the latest versions of Windows, this limitation can be expanded to approximately 32,000 characters. Your administrator will need to activate the “Enable Win32 long paths” group policy, or set the registry value
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabledto1.This allows the
open()function, theosmodule and most other path functionality to accept and return paths longer than 260 characters when using strings. (Use of bytes as paths is deprecated on Windows, and this feature is not available when using bytes.)After changing the above option, no further configuration is required.
Changed in version 3.6: Support for long paths was enabled in Python.