0

The initial trouble I encountered was the pip failing to install tensorflow lib due to Windows MAX_PATH restriction of 260 characters. Here's the message I received:

ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory:
'C:\\Users\\Root\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\
local-packages\\Python39\\site-packages\\tensorflow\\include\\external\\com_github_grpc_grpc\\
src\\core\\ext\\filters\\client_channel\\lb_policy\\grpclb\\client_load_reporting_filter.h'
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

I looked up the hint page that pip suggested. This linked to the Microsoft docs page which explained that the restriction could be lifted via setting the REG_DWORD registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled to 1.
After the amendment I made to the registry key, I rebooted the system and tried to install the tensorflow again. It didn't work - I received the same error message. Then I got back to the Microsoft docs page and discovered that LongPathsEnabled option could also be activated via Group Policy editor (aka gpedit.msc). There I went straight to Computer Configuration > Administrative Templates > System > Filesystem > Enable Win32 long paths and set the policy to be Enabled (it was Not Configured initially, despite the registry key being set). Then I rebooted my PC again, opened cmd and typed pip install tensorflow once again. Got the same error message.
This is where things started to get interesting: I was able to find out that LongPathsEnables option doesn't seem to work on my PC at all. Here's what Microsoft docs say:

These are the file management functions that no longer have MAX_PATH restrictions if you opt-in to long path behavior: CopyFileW, CopyFile2, CopyFileExW, CreateFileW, CreateFile2...

So I opened Visual Studio and wrote this code:

int main(int argc, char ** argv)
{
    HANDLE fileHandle = CreateFile2(
        L"C:\\Users\\Root\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\tensorflow\\include\\external\\com_github_grpc_grpc\\src\\core\\ext\\filters\\client_channel\\lb_policy\\grpclb\\client_load_reporting_filter.h",
        GENERIC_ALL,
        0,
        CREATE_ALWAYS,
        NULL
    );

    if (fileHandle == INVALID_HANDLE_VALUE)
    {
        DWORD error = GetLastError();
        cout << "CreateFile2 failed with LastError code: " << error << endl;
    }
    else
    {
        cout << "Success" << endl;
    }

    getchar();

    return 0;
}

And this failed with GetLastError() code 3 (ERROR_PATH_NOT_FOUND - The system cannot find the path specified). It is to be noted that when I shortened the path to C:\\Users\\Root\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\tensorflow\\include\\external\\com_github_grpc_grpc\\src\\core\\ext\\filters\\client_channel\\client_load_reporting_filter.h which resulted in 252 characters long path, it succeeded so I deduced that CreateFile2 also doesn't seem to have identified LongPathsEnabled option being set. The cause of the issue with the C++ program might be that there should be a longPathsAware element set to true in its application manifest (this is stated in the same Microsoft docs page I've referred to above), but I don't have a clue on how to set this option since it should have worked right after setting the registry key and rebooting my PC. I recall that I experienced the same issue about a half year ago on my previous Windows 10 installation and it got fixed super easily - LongPathsEnabled set to 1, PC rebooted and pip install <some-package> succeeded. This answer: Filename too long in Git for Windows implies that some programs have the Long Paths feature disabled by default but I have no idea if this is the case and even if it is, how to set this for pip. By the way, pip config set <name> <value> failed due to pip.ini file being inaccessible for writing (opened in PID: 0).
Tried manually create the file (client_load_reporting_filter.h), copying the original contents from the internet and it didn't help either. When I opened it in my notepad++, the path string was C:\Users\Root\AppData\Local\Packages\PYTHON~1.9_Q\LOCALC~1\LOCAL-~1\Python39\SITE-P~1\TE1A26~1\include\external\COM_GI~1\src\core\ext\filters\CLIENT~1\LB_POL~1\grpclb\CLIENT~1.H so notepad++ also seems to be unable to work with Long Paths.

I am completely clueless and desperate right now so I decided to ask a question here hoping that somebody have experienced this and might be able to help. I'll be happy if the issue can be resolved without reinstalling Windows, but this all seems so weird.

RedRidingHood
  • 568
  • 3
  • 16
  • 2
    Unfortunately, the restriction can be lifted, but many programs still don't work correctly in Windows if you have filenames longer than MAX_PATH. With regards to your C++ code - the behaviour varies depending on your version of Windows according to the docs. You might also need to escape the backslashes. – Ryan Pepper Nov 16 '21 at 08:18
  • @RyanPepper thank you for clarification. I downgraded python to version 3.7 and `tensorflow` installed successfully. – RedRidingHood Nov 17 '21 at 18:49
  • That should be a bug or you did something wrong. If the MSDN says `CreateFile2()` supports long files with the mentioned setting, that can usually be trusted, in my experience. Have you tried locating existing bug tickets or filing one yourself? – Ulrich Eckhardt Nov 17 '21 at 19:56
  • @UlrichEckhardt not yet, is there a proper way to file one, that would consider my system's settings or some other important stuff? – RedRidingHood Nov 22 '21 at 04:07

1 Answers1

0

Okay guys I have solved the problem with tensorflow installation by downgrading Python to version 3.7. But the issue with CreateFile2 is still there so this part of my question is left unanswered.
Thanks to Ryan Pepper, who clarified the situation a little by mentioning the fact that many programs still do not properly support Windows Long Paths.

RedRidingHood
  • 568
  • 3
  • 16