Python Import Statement

Discussion in 'Python' started by jinal jhaveri, Jun 29, 2003.

  1. Hi I have two files
    say

    a.py

    b.py


    a.py has 3 classes

    A
    B
    C

    Now in b.py I want to instantiate an object of class B

    so this is what I do in file b

    from xyz.A import B (xyz is the directory where A is lying and the
    paths are set accordingly)

    but it gives me an error of the kind, object cannot be called?

    Any hints

    thank you
    J.
    jinal jhaveri, Jun 29, 2003
    #1
    1. Advertising

  2. jinal jhaveri

    Tim Roberts Guest

    jinal jhaveri <> wrote:
    >
    >Hi I have two files
    >say
    >
    >a.py
    >b.py
    >
    >a.py has 3 classes
    >
    >A
    >B
    >C
    >
    >Now in b.py I want to instantiate an object of class B
    >so this is what I do in file b
    >
    >from xyz.A import B (xyz is the directory where A is lying and the
    >paths are set accordingly)


    Python treats file names as case sensitive, so you probably want

    from xyz.a import B

    Do you have an __init__.py in directory xyz so Python knows it is a module?

    >but it gives me an error of the kind, object cannot be called?


    Not from that line, it doesn't. Show us the line where you try to
    instantiate it. This kind of thing should work:

    from xyz.a import B
    bb = B()
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Jun 29, 2003
    #2
    1. Advertising

  3. jinal jhaveri

    Miki Tebeka Guest

    Hello Jinal,

    > Hi I have two files
    > say
    >
    > a.py
    >
    > b.py
    >
    >
    > a.py has 3 classes
    >
    > A
    > B
    > C
    >
    > Now in b.py I want to instantiate an object of class B
    >
    > so this is what I do in file b
    >
    > from xyz.A import B (xyz is the directory where A is lying and the
    > paths are set accordingly)

    You don't need to add the diretory name, just the module name (assuming
    it's in sys.path)
    import a
    b = a.B()
    OR
    from a import B
    b = B()

    See also http://www.python.org/doc/current/tut/node8.html

    HTH.
    Miki
    Miki Tebeka, Jun 29, 2003
    #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. Replies:
    3
    Views:
    788
  2. Jay McGavren
    Replies:
    11
    Views:
    1,111
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,061
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    451
    Duncan Booth
    Jul 22, 2004
  5. Stéphane Klein
    Replies:
    0
    Views:
    262
    Stéphane Klein
    Jul 5, 2011
Loading...

Share This Page