从 C++ 调用 Python 脚本并使用其输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16962430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Python script from C++ and using its output
提问by learner
I want to call a python script from C++ and wish to use the output .csv file generated by this script back into C++. I tried this in main():
我想从 C++ 调用一个 python 脚本,并希望将此脚本生成的输出 .csv 文件使用回 C++。我在 main() 中试过这个:
std::string filename = "/home/abc/xyz/script.py";
std::string command = "python ";
command += filename;
system(command.c_str());
This does call and execute the python script.
这会调用并执行 python 脚本。
The printcommands in the Python are being executed. Things are being printed on the screen when the script is called. So far so good. However, it is not creating the .csv file (part of the same script).
的print在Python命令正在执行。调用脚本时,屏幕上正在打印内容。到现在为止还挺好。但是,它不会创建 .csv 文件(同一脚本的一部分)。
Example: I had a training.csvfile with 100 entries. I called the Python script, with little changes to the script so that the training.csvfile now should contain only 50 entries instead of 100. It's overwritten.However, no such thing happening. Rest of the commands in the script (print, etc) are working perfectly.
示例:我有一个training.csv包含100 个条目的文件。我调用了 Python 脚本,对脚本几乎没有改动,因此training.csv文件现在应该只包含 50 个条目而不是 100 个条目。它被覆盖了。然而,没有这样的事情发生。脚本中的其余命令(print等)运行良好。
The training.csvfile is to be read with C++ normally using fstreamand getline.
该training.csv文件将使用 C++ 读取,通常使用fstream和getline。
Any idea how to do it (using Linux)?
知道怎么做(使用Linux)吗?
回答by zmo
Here is a solution to embed the execution of your python module from within your C++ application. It's not better or worst than forking/executing your python script through a system call, it just is a different way to do it. Whether it is best or not depend on your context and usage.
这是从 C++ 应用程序中嵌入 Python 模块执行的解决方案。这并不比通过系统调用分叉/执行你的 python 脚本更好或更坏,它只是一种不同的方法。它是否最好取决于您的上下文和用法。
Some time ago I have coded a way to load python modules as plugins to a C++ application, here's the interesting part.
前段时间我编写了一种将 python 模块作为插件加载到 C++ 应用程序的方法,这是有趣的部分。
Basically, you need to #include <Python.h>, then Py_Initialize()to start your python interpreter.
基本上,您需要#include <Python.h>, 然后Py_Initialize()启动您的python解释器。
Then you do import sys, using : PyRun_SimpleString("import sys");, and you can load your plugin by doing PyRun_SimpleString('sys.path.append("path/to/my/module/")').
然后你做import sys,使用 : PyRun_SimpleString("import sys");,你可以通过做加载你的插件PyRun_SimpleString('sys.path.append("path/to/my/module/")')。
To exchange values between C++ and Python, things get harder, you have to to transform all your C++ objects into python objects (starting line 69 in my script).
要在 C++ 和 Python 之间交换值,事情变得更难了,您必须将所有 C++ 对象转换为 python 对象(从脚本中的第 69 行开始)。
Then you can call your function using PyObject_Call_Object(...), using all the python objects you created as arguments.
然后你可以使用PyObject_Call_Object(...)你创建的所有python对象作为参数调用你的函数。
You get the return value, and transforms all those values in C++ objects. And don't forget the memory management in all that!
您获得返回值,并在 C++ 对象中转换所有这些值。并且不要忘记所有这些内存管理!
To end your python interpreter, a simple call to Py_Finalize().
要结束您的 Python 解释器,只需简单地调用Py_Finalize().
It really looks harder than it is really, but you have to be really careful doing this, because it could lead to leaks, security issues etc..
它看起来确实比实际更难,但是您必须非常小心地执行此操作,因为它可能导致泄漏、安全问题等。
回答by Vovanium
Try using POSIX's popen()instead of system(). It pipes stdin/stdout of child process to returned file handle.
尝试使用 POSIXpopen()而不是system(). 它将子进程的 stdin/stdout 管道到返回的文件句柄。
FILE* in = popen(command.c_str(), "r");
fscanf(in, ... // or some other method of reading
pclose(in);

