如何从 c++ 调用 python
How to call python from c++
我是 Python 新来者。我尝试像这样从c ++调用python脚本(在Raspberry Pi中(。
std::string pythonCommand = "python Callee.py ""+a+"" "+b;
int res = system(pythonCommand.c_str());
跑步后我得到了这个。
python: can't open file 'Callee.py': [Errno 2] No such file or directory
但是我可以成功地使用命令行运行 Callee.py 并且两个文件都存储在同一个目录中。
我错过了什么?
您可能正在某个奇怪的目录中运行python解释器(以及您的python Callee.py
命令((即在某个其他目录中,而不是您所期望的目录(。
你可以在调用 system(3( 之前使用 getcwd(3( 来查找你当前的工作目录。
你可以使用 chdir(2( 系统调用 (在调用 system
之前( 将目录更改为适当的目录。也许看到这个。
我还建议阅读高级 Linux 编程
另请阅读有关扩展和嵌入 Python 解释器的信息;但如果您需要嵌入一些解释器,请考虑 Guile & Lua。
你可以尝试这样的东西
system("/work/test/pythonscript.sh")
并在此脚本中定义如何执行/调用 Python 脚本。
这样您就不会被格式错误(c_string(( 和"\r"或依赖于操作系统的行尾(绊倒(
相关文章:
- 从C++调用 Python 并访问通过 Anaconda 安装的软件包
- 使用C++指针调用 python 函数
- 使用 pybind11 调用 Python 函数时避免复制输入数据
- 从C++调用 Python 脚本以持续流式传输数据
- 通过std::system调用python时出错
- 用c++调用python程序
- 在 C 代码中调用 Python 函数时第三次出现访问冲突写入位置错误
- 如何分发调用Python C++应用程序?
- 有没有办法用c ++ Gsoap客户端调用python soap spyne服务器?
- 使用来自C++的任意数量的参数(在编译时未知)调用 Python 函数
- 如何从 c++ 调用 python
- 从C++调用 Python 或 Lua 来计算表达式,仅在需要时计算未知变量
- 使用Tensorflow/Bazel从C++调用Python时出现链接错误
- 如何避免在Qt项目中调用Python脚本时出现分段错误
- 如何在C++环境中使用 Tensorflow 库调用 python 文件
- 从C++调用 Python 类方法,如果给定一个初始化的类作为 PyObject
- 使用 pybind11 从 C++ 调用 Python 函数
- 从C 中的多个线程调用Python函数
- numpy.core.multiarray在通过Xcode中开发的C 应用程序调用Python模块时未能导入
- 在QT Creator中使用C++调用Python脚本