Calling python function from C and import questions

S

sndive

Is there a better way to make a call from C than

PyRun_SimpleString("import
foo_in_python\nfoo_in_python.bar(whatever)\n");

?

I already imported the foo_in_python using PyImport_ImportModule
and wonder why do I need to keep importing it every time I'm
calling a python function in that module.

I stepped thru PyImport_ImportModule and it seems rather expensive.

I assume the module will end up in cache after PyImport_ImportModule
so that subsequent
PyRun_SimpleString("import foo_in_python\n...
is cheaper but I still have to wonder if I what I'm doing is the right
thing or there is a much simpler way of doing this.

Does anyone know why does the import tries to load .py files before
..pyc?
Thank you.
 
G

Gabriel Genellina

Is there a better way to make a call from C than

PyRun_SimpleString("import
foo_in_python\nfoo_in_python.bar(whatever)\n");

I already imported the foo_in_python using PyImport_ImportModule
and wonder why do I need to keep importing it every time I'm
calling a python function in that module.

Sure. Use the reference to the module that you got from
PyImport_ImportModule; you call a method using PyObject_CallMethod or
similar.
Does anyone know why does the import tries to load .py files before
.pyc?
It has to be sure that the .pyc is up-to-date; the .pyc contains a magic
number and the modification time of the corresponding .py
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top