[C++-sig] ImportError: timemodule.so

RICH JOHNSON richjohnson905 at msn.com
Mon Aug 7 15:07:17 CEST 2006


I am new to boost python and am having difficulty importing python modules 
into a little C++ program.  Any insight into the problem would be 
appreciated.

OS: Linux Redhat
gcc: 3.2.3
python 2.2
boost 1.33.1

>From the command line I can do:
[rjohnson at pitwar7 rjohnson]$ python
Python 2.2.3 (#1, Feb 15 2005, 02:41:06)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>from time import time,ctime
>>>print 'Today is', ctime(time())
Today is Fri Aug  4 10:47:20 2006
>>>

But the C++ program below outputs:
25
20
Traceback (most recent call last):
File "<string>", line 1, in ?
ImportError: /usr/lib/python2.2/lib-dynload/timemodule.so: undefined symbol: 
PyDict_SetItemString


#include <boost/python.hpp>
#include <iostream>

int main(int, char **)
{
using namespace boost::python;

Py_Initialize();

try
{
  PyRun_SimpleString("result = 5 ** 2");
     object module((handle<>(borrowed(PyImport_AddModule("__main__")))));
  object dictionary = module.attr("__dict__");
  object result = dictionary["result"];
  int result_value = extract<int>(result);
     std::cout << result_value << std::endl;
     dictionary["result"] = 20;

  PyRun_SimpleString("print result");

  // Here is the same command that ran successfully at the command line 
above but fails from  C++
  PyRun_SimpleString("from time import time,ctime\n"
                   "print 'Today is',ctime(time())\n");
}
catch (error_already_set)
{
  PyErr_Print();
}

Py_Finalize();
return 0;
}

My LD_LIBRARY_PATH is /usr/local/lib:/usr/lib/python2.2/config
PYTHONPATH is /usr/lib/python2.2/config





More information about the Cplusplus-sig mailing list