PyImport_Import() fails - what am I doing wrong here?

Bob Hepple bhepple at
Tue Feb 25 01:16:20 CET 2003

Apologies for re-posting this but I must have obfuscated it badly the
first time. Please allow me to re-state the problem - this is driving
me nuts.

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

It runs fine when called from python:

import foobar

All seems peachy. 

However when I try to call it from an embedded python interpreter in a C
program, 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
put a printf() call there to check). There are no error messages from
ld or python.

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.

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 reference 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/ ->

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

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



Bob Hepple
mailto:bhepple at

More information about the Python-list mailing list