0

I am writing tool that use subprocess.call and subprocess.Popen, but faced with an issue on Windows 10 that when env["PATH"] variable exceed size 8191~8192 characters subprocess cannot find program located in env["PATH"]:

`cmake` is not recognized as an internal or external command,
operable program or batch file.

Looks like CreateProcess on Windows that is used underhood do not parse such long env["PATH"]

I have found lots of similar issues on internet and looks like it is restriction on Windows with maximum command line length, but unfortunately I have not found how to fix it ...

Have somebody faced with the similar issue ?

Is there a way to modify registry to increase this max length ?

Or do you know about some kind of workaround to fix it on Windows except of removing some paths from env["PATH"] ?

Denis Kotov
  • 857
  • 2
  • 10
  • 29
  • Can you work around by supplying the absolute path to `cmake`? – Hai Vu Nov 12 '22 at 16:25
  • Yes, there is *Windows PATH variable size limit* (you can google for the emphasized string). Maybe that there is some duplicity in your `PATH` variable. Check e.g. as follows: `y = [x.rstrip('\\') for x in os.environ["PATH"].split(';')]; len(y); len(set(y))` - and/or there are some entries of no use (some installers/uninstallers does not purge the `PATH` properly). Check e.g. using my [`TestPath.bat` script](https://stackoverflow.com/a/42148234/3439404) (its usage see [here](https://superuser.com/a/1527258/376602)). – JosefZ Nov 12 '22 at 16:40
  • If I understand it correctly, you don't have any problem invoking whatever command from the command prompt, but you do within Python. How about `Popen(..., shell=True)`? Does it help? – Hai Vu Nov 13 '22 at 00:27

1 Answers1

0

I don't use Windows, so these suggestions are just a shot in the dark. The first suggestion is to use the absolute path to cmake as I stated in the comment.

The second is to use shutil.which() to locate the path to cmake and use that:

import shutil

cmake_path = shutil.which("cmake")
assert cmake_path is not None, "It seems shutil.which could not find cmake"

# Use cmake_path in subproces, instead just "cmake"
Hai Vu
  • 37,849
  • 11
  • 66
  • 93