如何运行使用PyRun读入std :: string的python文件 [英] How do I run a python file that is read into a std::string using PyRun

查看:252
本文介绍了如何运行使用PyRun读入std :: string的python文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Python嵌入到我的C ++程序中,并且已经使用了PyRun_SimpleString,但现在遇到了麻烦。

I am embedding Python into my C++ program, and have used PyRun_SimpleString quite effectively but now am having trouble.

我所做的是加载一个python.py文件一个std :: string,但现在有麻烦运行它。 PyRun_SimpleFileEx似乎没有做的伎俩,所以一些帮助将是巨大的!

What I have done is loaded a python.py file a std::string but am now having troubles running it. PyRun_SimpleFileEx didn't seem to do the trick either so some help would be great!

    std::string content;
    if(!ail::read_file(python_script, content))
    {
        error("Failed to load Python script \"" + python_script + "\"");
        return false;
    }

    if(prompt_mode)
        initialise_console();

    content = ail::replace_string(content, "\r", "");

    Py_Initialize();
    initialise_module();

    std::string script_directory;
    if(get_base_name(python_script, script_directory))
        PyRun_SimpleString(("import sys\nsys.path.append('" + script_directory + "')\n").c_str());

        write_line("Script dir: " + script_directory);
        ////-python_script H:\\CRAW\\craw\\script\\craw.py
        //content.c_str()

    //FILE *fp;
    //fp = fopen("H:\\CRAW\\craw\\script\\craw.py", "r");

    //PyRun_SimpleFileEx(fp, "craw.py", 1);
    if(PyRun_SimpleString(content.c_str()) != 0)
    {
        write_line("The main Python script contained errors.");
        return false;
    }

    //PyRun_SimpleString(("execfile('" + ail::replace_string(python_script, "\\", "\\\\") + "')").c_str());

    return true;


推荐答案

我通过使用字符串向量和阅读解决了我的问题每一行的文件放入向量,然后使用PyRun_SimpleString执行每一行。

I solved my problem by using a string vector and reading each line of the file into the vector, then executing each one using PyRun_SimpleString.

这里是完成的代码,没有错误检查。
std :: vector string_vector;
std :: string content;
if(python_script.empty())
return true;

Here's the finished code, no error checking though. std::vector string_vector; std::string content; if(python_script.empty()) return true;

    ail::read_lines(python_script, string_vector);

    if(!ail::read_file(python_script, content))
    {
        error("Failed to load Python script \"" + python_script + "\"");
        return false;
    }

    if(prompt_mode)
        initialise_console();

    content = ail::replace_string(content, "\r", "");

    Py_Initialize();
    initialise_module();

    std::string script_directory;
    if(get_base_name(python_script, script_directory))
        PyRun_SimpleString(("import sys\nsys.path.append('" + script_directory + "')\n").c_str());

    for(int i = 0; i < string_vector.size(); i++)
    {
        string_vector[i] = ail::replace_string(string_vector[i], "\r", "");
        PyRun_SimpleString(string_vector[i].c_str());
    }

    return true;

这篇关于如何运行使用PyRun读入std :: string的python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆