importing modules question

Discussion in 'Python' started by warhero, Oct 18, 2007.

  1. warhero

    warhero Guest

    Hey all, sorry for the totally newb question. I recently switched over
    to python from ruby. I'm having problems figuring out how module
    importing works.. as a simple example I've got these files:

    /example/loader.py
    /example/loadee.py

    loadee.py
    class loadee(object):
    def __init__(self):
    print "loadee"

    loader.py
    import loadee
    if __name__ == "__main__":
    l = loadee()


    When I run the loader file I get a TypeError: TypeError: 'module'
    object is not callable

    hm. Sorry, this is totally newb, this is the only switching pains I've
    had.
     
    warhero, Oct 18, 2007
    #1
    1. Advertising

  2. warhero

    warhero Guest

    got it figured out. nevermind.
     
    warhero, Oct 18, 2007
    #2
    1. Advertising

  3. warhero

    Amit Khemka Guest

    On 10/18/07, warhero <> wrote:
    > Hey all, sorry for the totally newb question. I recently switched over
    > to python from ruby. I'm having problems figuring out how module
    > importing works.. as a simple example I've got these files:
    >
    > /example/loader.py
    > /example/loadee.py
    >
    > loadee.py
    > class loadee(object):
    > def __init__(self):
    > print "loadee"
    >
    > loader.py
    > import loadee
    > if __name__ == "__main__":
    > l = loadee()
    >
    >
    > When I run the loader file I get a TypeError: TypeError: 'module'
    > object is not callable


    'import module' statement imports the 'module' object and binds the
    name to the local namespace. Any names defined in the module will have
    to referenced by module.module_object

    so you need to do something like this:
    import loadee
    if __name__ == "__main__":
    l = loadee.loadee()

    Alternatively you can directly import the objects defined in a module by :

    from loadee import loadee
    if __name__ == "__main__":
    l = loadee()

    cheers,
    --
    --
    Amit Khemka
     
    Amit Khemka, Oct 18, 2007
    #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. Tobiah
    Replies:
    2
    Views:
    320
    Tobiah
    Sep 14, 2003
  2. pythos
    Replies:
    3
    Views:
    342
    Jeremy Bowers
    Aug 22, 2004
  3. plb
    Replies:
    2
    Views:
    361
  4. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    308
    Peter Peyman Puk
    Mar 19, 2010
  5. cronoklee
    Replies:
    1
    Views:
    214
    shearichard
    Dec 17, 2010
Loading...

Share This Page