微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python subprocess.call找不到Windows Bash.exe

我有一个程序,从另一个程序运行在Linux的新的Windows子系统上运行。 我已经写了一个从Windows系统运行的Python程序,但会使用pythonsubprocess模块执行linux程序。 如果这是令人困惑的,请参阅下面的示例。

但是,当我这样做时,我发现,通过pythonsubprocess调用时,Windows无法findbash程序。

在Windows命令行或PowerShell的例子:

C:>bash -c "echo Hello World!" Hello World! C:>python Python 2.7.5 (default,May 15 2013,22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help","copyright","credits" or "license" for more @R_760_4045@ion. >>> import subprocess as s >>> s.call('bash -c "echo hello world"'.split()) Traceback (most recent call last): File "<stdin>",line 1,in <module> File "c:Python27-32libsubprocess.py",line 524,in call return Popen(*popenargs,**kwargs).wait() File "c:Python27-32libsubprocess.py",line 711,in __init__ errread,errwrite) File "c:Python27-32libsubprocess.py",line 948,in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified >>> s.call('bash -c "echo hello world"'.split(),shell=True) 'bash' is not recognized as an internal or external command,operable program or batch file. 1

我想也许这不是加载我的path设置,所以我把这个bash程序的完整地址

setfacl:关于选项

从C ++程序返回值到bash脚本

有没有更好的方法从ARP表获取MAC地址?

如何创build一个像$ RANDOM这样的bashvariables

在bash(Linux)中查找另一个csv的值(如vlookup)

>>> s.call(b,shell=True) 'C:WindowsSystem32bash.exe' is not recognized as an internal or external command,operable program or batch file. 1

编辑:我意识到我的命令可能会给一个问题,因为我分裂的空间和“回声你好世界”是一个参数,但尝试相同的东西与bash -c ls也给出了相同的错误

在没有临时(中间)文件的情况下处理相同的文件

如何即时显示exec-maven-plugin的输出

cd / bash在符号链接上的行为

如何使回声与bash中的读取兼容?

为什么在Bash中的'有一个空格?

对于在WOW64子系统中运行的32位程序,“System32”目录被重定向到“SysWOW64”。 WSL bash.exe加载程序以64位可执行文件的形式分发,因此从32位Python开始,需要使用虚拟的“SysNative”目录。 例如:

import os import platform import subprocess is32bit = (platform.architecture()[0] == '32bit') system32 = os.path.join(os.environ['SystemRoot'],'SysNative' if is32bit else 'System32') bash = os.path.join(system32,'bash.exe') subprocess.check_call('"%s" -c "echo 'hello world'"' % bash)

请注意,当前Windows管道未桥接到WSL管道,因此如果尝试使用stdout=PIPE或subprocess.check_output ,则WSL bash加载程序将失败。 您可以通过ReadConsoleOutputCharacter直接读取控制台输出(例如,看到这个答案 )。 或者,更简单地说,您可以将输出重定向到临时文件,将临时文件的路径作为WSL路径转换。 例如:

import tempfile def wintolin(path): path = os.path.abspath(path) if path[1:2] == ':': drive = path[:1].lower() return '/mnt/' + drive + path[2:].replace('\','/') cmd = '"%s" -c "echo 'hello world' > '%s'"' with tempfile.NamedTemporaryFile(mode='r',encoding='utf-8') as f: subprocess.check_call(cmd % (bash,wintolin(f.name))) out = f.read()

编辑:从Windows生成14951,你应该能够使用stdout=PIPE 。 请参阅WSL博客文章Windows和Ubuntu互操作性 。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐