Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python shutil.exe不与wsl.exe一起工作_Python_Windows_Python 3.x_Shell_Shutil - Fatal编程技术网

Python shutil.exe不与wsl.exe一起工作

Python shutil.exe不与wsl.exe一起工作,python,windows,python-3.x,shell,shutil,Python,Windows,Python 3.x,Shell,Shutil,我正在尝试使用shutil.which检查Linux子系统是否安装在Windows 10上 使用命令提示符中的Windowswhere命令,我可以看到wsl.exe可执行文件的位置 C:\Users\spike>where wsl C:\Windows\System32\wsl.exe 上面显示WSL确实存在,并且在我的系统PATH中。 当我在Python中使用which函数时,它表示找不到可执行文件 print(which("wsl")) # Returns None 为了确保哪个

我正在尝试使用
shutil.which
检查Linux子系统是否安装在Windows 10上

使用命令提示符中的Windows
where
命令,我可以看到
wsl.exe
可执行文件的位置

C:\Users\spike>where wsl
C:\Windows\System32\wsl.exe
上面显示WSL确实存在,并且在我的系统
PATH
中。 当我在Python中使用
which
函数时,它表示找不到可执行文件

print(which("wsl"))  # Returns None
为了确保
哪个
有效,我在
cmd.exe
上测试了它

print(which("cmd"))  # Returns "C:\Windows\System32\cmd.exe"
这很有效。好吧,如果我用有效的命令进行系统shell调用呢

退出代码1,未找到命令
wsl
。 所以我再次在
cmd.exe
上测试它

print(system("where cmd"))  # Returns 0
好吧,那就行了。有什么问题

对于每个Python3示例,假设这些导入

from shutil import which
from os import system
为什么Python不能找到
wsl.exe
,即使它已经被证明存在


谢谢。

感谢@eryksun,他在评论中帮助解决了这个问题

问题是我使用的是32位Python,
wsl.exe
仅在
C:/Windows/System32
中。问题是Python在
C:/Windows/SysWOW64
中查找可执行文件

wsl.exe
仅为64位,您正在查看的是
SysWOW64
而不是真正的
System32
,因为您使用的是32位Python埃尔克森

因为WSL只支持64位系统,所以我最终只使用64位Python运行代码。但是,如果只使用Py32,另一种解决方案是使用系统根环境变量和os.path.join直接访问
SysWOW64

在Windows 7+中,实
System32
目录在32位进程中可作为“SysNative”访问。不幸的是,此虚拟目录在本机64位进程中不可用,因此需要首先检查它是否存在。例如:
sysnative=os.path.join(os.environ['SystemRoot','sysnative');如果os.path.exists(sysnative):…
–埃尔克森


现在您可以在64位中安装python,一切都将正常工作。(请注意,当python安装程序运行时,应该编写64位的软件包和其他东西,因为64位[加载栏所在的位置])

wsl.exe只有64位,并且您正在查看SysWOW64而不是真正的System32,因为您使用的是32位python。@eryksun Ahhh这确实解释了这一点。你有办法解决这个问题吗?你应该发布一个答案,我会接受的。在Windows7+中,真正的System32目录在32位进程中可以作为“SysNative”访问。不幸的是,此虚拟目录在本机64位进程中不可用,因此需要首先检查它是否存在。例如:
sysnative=os.path.join(os.environ['SystemRoot','sysnative')
如果os.path.存在(sysnative):…
from shutil import which
from os import system