Embedding Python

Sun, 04 Oct 2009

A quick pointer for anyone who is embedding python into a C application. I found that the useful looking example code didn't work as described. When I ran it I would get:

$ ./call multiply multiply 3 2
ImportError: No module named multiply
Failed to load "multiply"

Even though I could run:

$ python
>>> import multiply
>>> multiply.multiply(3, 2)
Will compute 3 times 2
Result of call: 6

The problem is that sys.path is different between the two environments. In the C code, it doesn't include the current directory. You can fix this easily:

Py_Initialize();
PyObject *psyspath = PySys_GetObject("path");
PyList_Insert(psyspath, 0, PyString_FromString(""));

Hoepfully this will be helpful to someone.

I haven't finished the application that is actually using this (I'll post it when done). But I'm not crazily impressed by the Python C-API. A few examples:

  • The fact that I'm writing this post (maybe it is something different in the Ubuntu python distribution)
  • Not enough consts: Both PyImport_ImportModuleLevel() and PyImport_ImportModuleEx() take "char *name" as a parameter. If they do modify the values pointed to, then it isn't documented how. If they don't, then they should be marked const.
  • Inconsistent naming: PyImport_Cleanup() is documented as "For internal use only". Nearly every other internal function (like _PyImport_Init) has a name starting with an underscore.
  • python-config: It would be much better to use pkg-config like every other library. It would also be nice not to add all sorts of flags which have nothing to do with compiling applications that use this library. For example, the --cflags option adds "-g -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing" - none of which should be needed. Also, running "python-config --cflags --ldflags" silently ignores the "--ldflags" option.
  • Bug reporting non-obvious. The number 1 hit on google for me for "python-config cflags" is the bug report for the previous point. Maybe I'm dumb, but I can't see any way of commenting on this page, to help out.

I'd like it if things got more pleasant from here, but I'm not holding my breath.

One thing that is particularly pleasant is the IRC channel. That was most helpful for working out what was wrong with the example code.
9 minutes after story
Name & email are optional. Email will not be obfuscated.
HTML tags will be removed except hyperlinks.
 

About

I'm a nerd living in Sydney. This is a place where I can write stuff about my interests and not care that no one else is reading.

I like music, maths, programming, pretty pictures, filters and other good things.

(more info)

It should be fairly obvious that this isn't connected to my employer at all.

Email me (not a catchpa)

Email policy

Subscribe

RSS Feed RSS

Get an aggregator

Liferea (Linux)

Vienna (OSX)

Feedreader (Windows)

Google Reader (Web based)

I've only used Liferea, so I can't vouch for the other ones.

About this site

This site runs a (modified) version of blosxom.

The host is GeekISP, and they seem to do an excellent job.