【发布时间】:2013-04-14 18:59:03
【问题描述】:
我有一个运行lessc的函数(与npm install -g less一起安装):
>>> import subprocess
>>> subprocess.Popen(['lessc'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
不幸的是,除非我添加shell=True,否则它不起作用:
>>> subprocess.Popen(['lessc'], shell=True)
<subprocess.Popen object at 0x01F619D0>
如何在不使用shell=True 的情况下使lessc 运行?
【问题讨论】:
-
您能否在不使用
shell=True的情况下使用其完整路径名显式调用二进制文件? -
@kragniz:
lessc是一个批处理文件(这实际上可能是问题所在),当我使用完整路径运行它时会出现同样的错误。 -
错误是子流程模块本身产生的还是批处理脚本产生的?您可以在 Popen 调用中添加
cwd=r'c:\path\to\script\'参数 -
@JBernardo:
subprocess模块。因此,如果不编辑脚本本身(它是 Web 框架的资产捆绑器的一部分),就无法解决这个问题? -
The docs:在带有
shell=True的 Windows 上,COMSPEC 环境变量指定默认 shell。唯一需要在 Windows 上指定shell=True的情况是当您希望执行的命令内置到 shell 中时(例如 dir 或 copy)。您不需要shell=True来运行批处理文件或基于控制台的可执行文件。
标签: python windows shell subprocess