HELP! importing python module into C++ mystery

jeannie jeannieyang at hotmail.com
Mon Feb 3 19:53:41 EST 2003


hi all,

i have a bit of a mystery at hand.  i'm sure i'm missing something
very simple, hope someone out there can bonk me on the head and tell
me what i'm doing wrong.

i am trying to embed a python module into my c++ application.  i have
been using the Py_GetPath and PySys_SetPath song and dance rather
successfully.  i.e., it's been importing my module and working with my
application just beautifully.

but today, for no apparent reason and seemingly unprovoked, it just up
and stopped working.  called it quits without alerting me.  and
refuses to import my module anymore.

i have the following code:
  Py_Initialize();

  char *oldpath, *newpath, *blah;
  oldpath = Py_GetPath();
  cerr << "old setpath = " << oldpath << endl << endl;

  newpath = new char[strlen(oldpath)+4];
  strcpy(newpath, oldpath);
  strcat(newpath, ":.");  // ":." for unix, or ";." for windows
  cerr << "new setpath = " << newpath << endl;
  PySys_SetPath(newpath);
  delete [] newpath;

  blah = Py_GetPath();
  cerr << "new sys path = " << blah << endl;

  // Import the Python Module
  pModule = PyImport_ImportModule("mymodule");

the annoying thing is, i look in my directory, and a mymodule.pyc file
is generated, somehow implying that the program did manage to get
somewhere in finding mymodule.  however, PyImport_ImportModule returns
NULL.

second, when i call Py_GetPath again, it does not show the newly
appended "." directory, even though newpath prints out correctly.

so fine.  i try setting PYTHONPATH to "."  that doesn't work.

last ditch, i even copy mymodule.py file into the
/usr/local/lib/python2.2 directory.  still no dice.

what is going on?  what am i missing?  it was working beautifully and
i didn't change anything with the module file or the code.

any help is really appreciated.  it's driving me bananas over here. 
i'm a two day newbie at this... be gentle.  :)

thanks,
jeannie
jeannieyang at hotmail.com




More information about the Python-list mailing list