xonsh "which" 等效 - 如何测试(子进程模式)命令是否可用?
xonsh "which" equivalent - how to test if a (subprocess mode) command is available?
我想测试(从 xonsh)命令是否可用。如果我在 xonsh 命令提示符下尝试此操作:
which bash
然后就可以了:
user@server ~ $ which bash
/usr/bin/bash
但它不适用于 xonsh 脚本:
#!/usr/bin/env xonsh
$RAISE_SUBPROC_ERROR = True
try:
which bash
print("bash is available")
except:
print("bash is not available")
因为它导致了这个错误:
NameError: name 'which' is not defined
我知道 which
是 shell 内置的。例如。它不是可执行文件。但它在 xnosh 命令提示符下可用。那为什么它在 xonsh 脚本中不可用?最终的问题是:如果(子进程模式)命令可用,我如何测试(从 xonsh 脚本)?
import shutil
print(shutil.which('bash'))
我想测试(从 xonsh)命令是否可用。如果我在 xonsh 命令提示符下尝试此操作:
which bash
然后就可以了:
user@server ~ $ which bash
/usr/bin/bash
但它不适用于 xonsh 脚本:
#!/usr/bin/env xonsh
$RAISE_SUBPROC_ERROR = True
try:
which bash
print("bash is available")
except:
print("bash is not available")
因为它导致了这个错误:
NameError: name 'which' is not defined
我知道 which
是 shell 内置的。例如。它不是可执行文件。但它在 xnosh 命令提示符下可用。那为什么它在 xonsh 脚本中不可用?最终的问题是:如果(子进程模式)命令可用,我如何测试(从 xonsh 脚本)?
import shutil
print(shutil.which('bash'))