Forcing a import.

G. David Kuhlman dkuhlman at netcom.com
Wed Oct 27 17:52:25 EDT 1999


Arinte (shouldbe at message.com) wrote:
> I have a app that embeds python.  I run the .py files with PyRun_AnyFile()
> right now, I was trying to so if there was a way I could force them to
> import my c methods.  I tried a PyRun_SimpleString before I do the anyfile
> call, but that did seen to work, because I got an attribute error.  Is a
> forced import that isn't written in the *.py possible.
> 
> 

Don't forget to initialize the your module:

    initMyModule();
    result = PyRun_SimpleString("import MyModule");

Then you might try to read in the file and run it as a string:

    char * aScript;
    /* Do something to read the file contents into aScript. */
    result = PyRun_SimpleString(aScript);

I'm pretty sure that this is effectively what I do, and it works.

See section 1.4 of "Extending and Embedding the Python Interpreter"
in the standard documentation for more info on initializing
extension modules.

Hope this helps.

By the way, what is the difference between:

    PyRun_AnyFile() vs. PyRun_SimpleFile() vs. PyRun_File(), and
    PyRun_String() vs. PyRun_SimpleString()? 

How do I decide between these?

I always use the "Simple" version.  If I'm doing OK, maybe I don't
need to know.

  - Dave

  - Dave




More information about the Python-list mailing list