naming the main module in embedded Python

Burton Samograd burton at
Wed Sep 22 00:26:33 CEST 2010

Tomasz Koziara <t.koziara at> writes:
> I am embedding Python as an interpret in my code. Now, whenever my
> code or Python itself issues an error/warning message I am getting
> something like:
> File "<string>", line 1, in <module>
> or
> __main__:46: RuntimeWarning: My warning message
> I am using PyRun_SimpleString to load part of the code and the I call:
> sprintf (line, "execfile ('%s')", path);
> error = PyRun_SimpleString (line).
> The question is: how can I set up the module name or input file name
> so that my error/warning messages output them rather than the above
> default values?

Use this rather than execfile:

    exec compile(code, filename, "exec")

You'll have to read the contents of the file into the string 'code'
first (unless compile will take a file object, which I'm not sure

Burton Samograd

More information about the Python-list mailing list