problem with import path on a python C extension

Discussion in 'Python' started by fredbasset1000@gmail.com, Feb 9, 2009.

  1. Guest

    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
    , Feb 9, 2009
    #1
    1. Advertising

  2. 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
    Carsten Haese, Feb 9, 2009
    #2
    1. Advertising

  3. 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
    Carsten Haese, Feb 9, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand
    Replies:
    3
    Views:
    878
    Tim Daneliuk
    Nov 8, 2003
  2. Stefan Seefeld
    Replies:
    3
    Views:
    949
  3. per9000
    Replies:
    7
    Views:
    398
    Magnus Lycka
    Feb 27, 2006
  4. mynthon
    Replies:
    5
    Views:
    1,999
    krowey
    Feb 9, 2013
  5. Jabba Laci

    import os or import os.path

    Jabba Laci, Sep 6, 2011, in forum: Python
    Replies:
    0
    Views:
    177
    Jabba Laci
    Sep 6, 2011
Loading...

Share This Page