Using dynamic libraries with Python C extensions in embedded Python apps fails

Bob Hepple bob_hepple at
Thu Feb 20 11:47:11 CET 2003

Yes - I'm trying to do just about everything at once here!

I have a Python extension module written in C. I want to use it as a
extension both in python scripts and in an embedded C app.

The module foobar lives in /usr/lib/python1.5/site-packages/
and is
built by in the usual way from foobar.c and a -l linker
to which itself lives in /usr/local/lib:

running install
running build
running build_ext
building 'foobar' extension
gcc -g -O2 -fPIC -I/usr/local/include -I/usr/include/python1.5 -c
foobar.c -o build/temp.linux-i686-1.5/foobar.o
gcc -shared build/temp.linux-i686-1.5/foobar.o -L/usr/local/lib
-lfoobar -o build/lib.linux-i686-1.5/
running install_lib
copying build/lib.linux-i686-1.5/ ->

It runs fine when called from python:

import foobar

All seems peachy. 

However when I try to call the foobar extension from an embedded
interpreter, the PyImport_Import() method fails:

#include <Python.h>
    pName = PyString_FromString("foobar");
	if (!pName) {
		goto exit_point;

    pModule = PyImport_Import(pName);
	... returns NULL

I tried PyImport_ImportModule("foobar") too with the same result.

The initfoobar() method in the extension module never gets called (I
a printf call there to check). There are no error messages from ld or

If I run 'strace' on the embedded version, I can see something
(presumably dlopen()) opening the extension module and (presumably a
linker) opening the shared library: ...

open("/usr/lib/python1.5/site-packages/", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0775, st_size=111607, ...}) = 0
open("/usr/lib/python1.5/site-packages/", O_RDONLY) = 4
read(4, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0@\23\0\000"...,
1024) = 1024
close(4)                                = 0
open("/usr/local/lib/", O_RDONLY) = 4
read(4, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\360d\1"...,
1024) = 1024
fstat64(4, {st_mode=S_IFREG|0775, st_size=2204306, ...}) = 0

Oddly, the foobar extension package is opened twice - presumably the
first time it is loading the modules and the second time it is the
linker trying to resolve externals. These are actually resolved later
when /usr/local/lib/ is opened.

Puzzling, to say the least? Any ideas out there?

Background - Linux RedHat 7.3 / python 1.5 (2.2 makes no difference)


More information about the Python-list mailing list