Python Import Statement

J

jinal jhaveri

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

Tim Roberts

jinal jhaveri said:
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()
 
M

Miki Tebeka

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top