Python Import Statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jinal jhaveri

    Python Import Statement

    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.



  • Tim Roberts

    #2
    Re: Python Import Statement

    jinal jhaveri <[email protected] u> wrote:[color=blue]
    >
    >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)[/color]

    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?
    [color=blue]
    >but it gives me an error of the kind, object cannot be called?[/color]

    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, [email protected]
    Providenza & Boekelheide, Inc.

    Comment

    • Miki Tebeka

      #3
      Re: Python Import Statement

      Hello Jinal,
      [color=blue]
      > 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)[/color]
      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

      Comment

      Working...