【发布时间】:2019-02-22 18:24:52
【问题描述】:
我知道有一些类似的问题,这里是Invoking C compiler using Python subprocess command 和subprocess, invoke C-program from within Python,但我相信我的问题在某种意义上是不同的。
我需要编译一个使用一些 ROOT 库的 c++ 程序,所以我需要添加一些标志并链接一些库以进行编译。因此,我在普通 shell 上的编译行是:
> $($ROOTSYS/bin/root-config --cxx) $($ROOTSYS/bin/root-config --cflags --glibs) Analysis.cxx -o analysis.exe
效果很好。我想从我的 python 脚本中进行编译。我已经阅读了subprocess 模块的文档,但是如果在 subprocess.Popen 的调用中不使用shell=True 就无法获得解决方案,而且我并没有真正理解其中的区别。如果我使用:
process = Popen(["$($ROOTSYS/bin/root-config --cxx) $($ROOTSYS/bin/root-config --cflags --glibs) Analysis.cxx -o analysis.exe"], shell=True)
完成这项工作。但是,这个:
process = Popen(["$($ROOTSYS/bin/root-config --cxx)", "$($ROOTSYS/bin/root-config --cflags --glibs)", "Analysis.cxx", "-o", "analysis.exe"])
我得到了以下信息:
Traceback (most recent call last):
File "make_posanalysis.py", line 45, in <module>
"Analysis.cxx", "-o", "analysis.exe"])
File "Python/2.7.15/x86_64-slc6-gcc62-opt/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "Python/2.7.15/x86_64-slc6-gcc62-opt/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我想了解使用/不使用shell=True 之间的区别,因为这似乎是使脚本工作与否的原因。或者,我还有什么遗漏的吗?
【问题讨论】:
标签: python python-2.7 shell subprocess root-framework