shutil.which() not finding programs without append

2020-04-17 04:09发布

Trying to use shutil.which() to determine if git is installed. From the docs, I see which() on Windows should use PATHEXT to know which file extensions to append when searching. However the following occurs when using the interpreter:

>>> import os
>>> import shutil
>>> os.getenv('PATHEXT')
'.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;'
>>> shutil.which('git')
>>> shutil.which('git.exe')
'C:\\Program Files\\Git\\cmd\\git.exe'

This is in a virtual environment created using VirtualEnv Wrapper in D:\Envs if that makes any difference.

1条回答
Animai°情兽
2楼-- · 2020-04-17 04:41

Remove the trailing ; from your PATHEXT environment variable. It seems shutil.which() gets confused by this and interprets an empty string as a valid file extension, and therefore doesn't try to append any of the file extensions.

More details here: "jupyter-kernelspec" not found while installing iqsharp however it exists on PATH

查看更多
登录 后发表回答