【问题标题】:Run python script from C++从 C++ 运行 python 脚本
【发布时间】:2016-02-05 05:22:11
【问题描述】:

我想从 c 运行一个 python 脚本。我尝试编译成功,但它给出错误“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;
 }

【问题讨论】:

  • 您的 C 程序从哪里(哪个目录)运行?尝试使用安装它的完整路径运行 python。
  • python 在标准程序搜索路径中吗?你能从命令提示符和任何目录中通过编写命令python 来启动 Python 吗?
  • @Joachim Pileborg 它位于标准程序搜索路径中。当我输入它时,它会给出 python 版本

标签: python c++


【解决方案1】:

您想使用 python 可执行文件的完整路径,例如:

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

【讨论】:

  • 仍然给出“系统找不到指定的路径”
  • 我只是举了一个例子,你必须使用你的路径
  • 我这样使用...... std::string filename = "C:/Users/BirunthaG/Desktop/print.py"; std::string 命令 = "C:/Python27/python"; @保罗埃文斯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多