Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I load and execute python script in C++ application
    primarykey
    data
    text
    <p>I want to extend my application, which is written in C++ using python scripts (extensions). I originally wanted to use TCL for that, just like they do in xchat, for example, but later I decided to use python, because it seems to be quite popular for whatever reasons.</p> <p>However, I am failing to load and execute even very simple python script. I followed <a href="http://docs.python.org/2/extending/embedding.html" rel="nofollow">http://docs.python.org/2/extending/embedding.html</a></p> <p>When I give a filename of script that I want to load as argument to <code>pName</code>, the error I get from <code>PyErr_Print</code> is: ImportError: Import by filename is not supported.</p> <p>Reading the documentation, I figured I might need to run <code>PyImport_ExecCodeModule</code>, however this C function requires 2 arguments, 1 is char * (probably a name of module), other one is compiled python code, which according to docs I can get by calling python function <code>compile()</code>. Unfortunately it doesn't say how do I call this python function using C api's in my C++ code. Ideally I would imagine to do something like</p> <pre><code>PyObject *code = PyCompile("print (\"hello :)\""); </code></pre> <p>but I couldn't find any function like PyCompile, neither any other C-api function that would simply allowed me to execute python internal function (like compile) and grab its output as <code>PyObject</code>.</p> <p>So, question is: how can I easily load a python script from a file (something.py) and execute it within my application using the embedded python interpretor?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload