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. Advertisements

  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. Advertisements

  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. Advertisements

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. Stefan Seefeld
    Replies:
    3
    Views:
    1,286
  2. per9000
    Replies:
    7
    Views:
    578
    Magnus Lycka
    Feb 27, 2006
  3. SMALLp

    Python import search path!

    SMALLp, Dec 14, 2007, in forum: Python
    Replies:
    3
    Views:
    649
    Bjoern Schliessmann
    Dec 14, 2007
  4. graph
    Replies:
    0
    Views:
    605
    graph
    Sep 26, 2008
  5. Kelly, Brian

    python import sys.path

    Kelly, Brian, Dec 30, 2008, in forum: Python
    Replies:
    1
    Views:
    2,524
    John Machin
    Dec 30, 2008
  6. Kelly, Brian

    FW: python import sys.path

    Kelly, Brian, Jan 2, 2009, in forum: Python
    Replies:
    2
    Views:
    1,017
    Fuzzyman
    Jan 2, 2009
  7. mynthon
    Replies:
    5
    Views:
    2,309
    krowey
    Feb 9, 2013
  8. Jabba Laci

    import os or import os.path

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