Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with import path on a python C extension

Reply
Thread Tools

problem with import path on a python C extension

 
 
fredbasset1000@gmail.com
Guest
Posts: n/a
 
      02-09-2009
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
 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      02-09-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> [...]
> 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,

--
Carsten Haese
http://informixdb.sourceforge.net
 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      02-09-2009
(E-Mail Removed) wrote:
> 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,

--
Carsten Haese
http://informixdb.sourceforge.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.path.getmtime on windows, error: 206 - path or extension too long mynthon Python 5 02-09-2013 12:25 AM
import os or import os.path Jabba Laci Python 0 09-06-2011 09:25 PM
Python import search path! SMALLp Python 3 12-14-2007 10:50 PM
problem(s) with import from parent dir: "from ../brave.py import sir_robin" per9000 Python 7 02-27-2006 06:36 PM
Problem with import "from omniORB import CORBA, PortableServer" Stefan Seefeld Python 3 04-11-2005 08:54 PM



Advertisments