2015-11-04 203 views
1

我想从c运行一个python脚本。我试过它编译成功,但它给错误“python不被识别为内部或外部命令,可操作的程序或批处理文件”。从C++运行python脚本

#include <iostream> 
using namespace std; 
// main() is where program execution begins. 

int main() 
{ 
    std::string filename = "hi.py"; 
    std::string command = "python "; 
    command += filename; 


    FILE* in = popen(command.c_str(), "r"); 
    pclose(in); 
    return 0; 
} 
+0

哪里(哪个目录)是C程序运行?尝试使用安装它的完整路径运行python。 –

+0

在标准程序搜索路径中是否是“python”?你能从命令提示符和任何目录中通过编写命令“python”来启动Python吗? –

+0

@ Joachim Pileborg它是在标准的程序搜索path.when我输入它给Python版本 –

回答

0

您想使用的完整路径你的Python可执行文件,是这样的:

std::string command = "/usr/bin/python "; 
+0

仍然给“系统找不到指定的路径” –

+0

我刚刚给出了一个*示例*,您必须使用*您的*路径 –

+0

我用这个...... std :: string filename =“C:/Users/BirunthaG/Desktop/print.py”; \t std :: string command =“C:/ Python27/python”; @保罗·埃文斯 –