Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Python Import Statement (http://www.velocityreviews.com/forums/t318974-python-import-statement.html)

jinal jhaveri 06-29-2003 02:28 AM

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 06-29-2003 05:58 AM

Re: Python Import Statement
 
jinal jhaveri <jhaveri@usc.edu> 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, timr@probo.com
Providenza & Boekelheide, Inc.

Miki Tebeka 06-29-2003 06:26 AM

Re: Python Import Statement
 
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


All times are GMT. The time now is 06:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.