problem with import path on a python C extension

F

fredbasset1000

Hi All,

I've written a simple Python extension module in C, but Python is
having problems trying to locate it when I import it into an existing
Python file. In my example, I wrote a C extension called "diomodule"
which exists in the directory : /root/project/drivers/dio/
diomodule.c. It has compiled fine and I end up with a diomodule.so
file in that same directory. I have problems when trying to import it
into another Python file however, e.g. in a file /root/project/daemon/
daemon.py I tried to import it with "import
project.drivers.dio.diomodule". Python throws an error however when I
try to call one of the functions:

File "/root/project/daemon/daemon.py", line 115,
in ?
diomodule.init
()
NameError: name 'diomodule' is not defined

I can only get it to work if I copy the .so file to the directory
where daemon.py is and change the import to "import diomodule".

So my question is why can't Python locate the new extension module
when I try to import it with "import project.drivers.dio.diomodule"?

Thanks for any responses,
Fred
 
C

Carsten Haese

[...]
So my question is why can't Python locate the new extension module
when I try to import it with "import project.drivers.dio.diomodule"?
This import statement binds the module to the name
"project.drivers.dio.diomodule". It does not bind anything to the name
"diomodule", which is the name you want to use to refer to the module.

To bind the "diomodule" name to this module, you can do one of two things:

1:
from project.drivers.dio import diomodule

2:
import project.drivers.dio.diomodule
diomodule = project.drivers.dio.diomodule

Hope this helps,
 
C

Carsten Haese

I can only get it to work if I copy the .so file to the directory
where daemon.py is and change the import to "import diomodule".

And to supplement my previous reply, another solution is to make sure
that /root/project/drivers/dio is in your PYTHONPATH. Then you can
simply "import diomodule" and refer to the module by the name "diomodule".

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top