Shutil.which() 没有附加文件扩展名就找不到程序 [英] shutil.which() not finding programs without appending file extension

查看:24
本文介绍了Shutil.which() 没有附加文件扩展名就找不到程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 shutil.which() 来确定是否安装了 git.从文档中,我看到 Windows 上的 which() 应该使用 PATHEXT 来知道搜索时要附加哪些文件扩展名.但是,在使用解释器时会发生以下情况:

<预><代码>>>>导入操作系统>>>进口商铺>>>os.getenv('PATHEXT')'.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;'>>>Shutil.which('git')>>>关闭.which('git.exe')'C:\\Program Files\\Git\\cmd\\git.exe'

这是在使用 VirtualEnv Wrapper 在 D:\Envs 中创建的虚拟环境中,如果这有什么不同的话.

解决方案

PATHEXT 环境变量中删除尾随的 ;.似乎 shutil.which() 对此感到困惑并将空字符串解释为有效的文件扩展名,因此不会尝试附加任何文件扩展名.

更多详情:jupyter-内核规范"安装 iqsharp 时未找到,但它存在于 PATH

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.

解决方案

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

这篇关于Shutil.which() 没有附加文件扩展名就找不到程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆