我有一个运行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>

我该怎么做才能在不使用lessc的情况下运行shell=True

最佳答案

将文件更改为lessc.bat,或创建调用lessc的.bat文件。这样,该文件将被Windows识别为批处理文件,并将正确执行。

除此以外,您可能还需要设置cwd,具体取决于.bat文件的位置。

09-13 13:35
查看更多