Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Import problem

Reply
Thread Tools

Import problem

 
 
Achim Domma (Procoders)
Guest
Posts: n/a
 
      11-22-2004
Hi,

in my __init__.py of module A.B if have something like:

from win32com.client import Dispatch
obj=Dispatch('...')
VERSION=tuple([int(x) for x in obj.Version.split('.')])
del obj

Now I should be able to write:

import A.B
if A.B.VERSION[0]=5:
do_something()

but

from A.B import VERSION

does not work. Mysterious to me, but not a real problem. But now I have
another problem. In my __init__.py I do something like this, after
setting up my VERSION variable:

import _C
classA = _C.classA
classB = _C.classB

In _C I have to do some stuff depending on VERSION. Therefore I have in
_C.py:

import A.B
if A.B.VERSION[0]==5: <- Error
do_something()

I get the error:
AttributeError: 'module' object has no attribute 'B'

Seems like I'm mixing up something in a very bad way, but I have no idea
what I'm doing wrong!?

regards,
Achim
 
Reply With Quote
 
 
 
 
Achim Domma (Procoders)
Guest
Posts: n/a
 
      11-22-2004
Achim Domma (Procoders) wrote:

> Seems like I'm mixing up something in a very bad way, but I have no idea
> what I'm doing wrong!?


I have moved the calculation of VERSION to a module _Version.py which
contains a function Version(), which is imported by __init__.py and
_C.py. That works and is much cleaner, but I would still be happy if
somebody could explain we, what's going wrong.

regards,
Achim
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      11-22-2004
Achim Domma (Procoders) wrote:

> Achim Domma (Procoders) wrote:
>
>> Seems like I'm mixing up something in a very bad way, but I have no
>> idea what I'm doing wrong!?

>
>
> I have moved the calculation of VERSION to a module _Version.py which
> contains a function Version(), which is imported by __init__.py and
> _C.py. That works and is much cleaner, but I would still be happy if
> somebody could explain we, what's going wrong.
>
> regards,
> Achim


Well, the big problem with your original formulation is your circular
imports. In brief ...

If A imports B, and B imports A, the copy of A that B sees is the
incompletely-executed definition at the time that the import of B was
started.

When you say in your first post """
Now I should be able to write:

import A.B
if A.B.VERSION[0]=5:
do_something()

but

from A.B import VERSION

does not work.""", are you trying to use both those formulations in the
same module? You do not explain what you mean by "does not work", so I
suspect that you are trying to use a package=based reference
(A.B.VERSION) when you have actually imported the VERSION variable into
your namespace.

sholden@dellboy ~
$ cat pkg/__init__.py
#!/usr/bin/python
objv = "2.3.45"
VERSION = tuple([int(x) for x in objv.split(".")])
del objv

sholden@dellboy ~
$ python -c "from pkg import VERSION; print VERSION"
(2, 3, 45)

The above is not an exact match for your code, but it shows that when
you use the "from" form of the import statement the imported variables
appear directly in the namespace of the importing module, so there is no
need to qualify them with the package name.

Hope this helps. If not, perhaps you could post some actual error
messages to explain what you mean by "does not work".

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
 
Reply With Quote
 
Nick Coghlan
Guest
Posts: n/a
 
      11-22-2004
Achim Domma (Procoders) wrote:
> Achim Domma (Procoders) wrote:
>
>> Seems like I'm mixing up something in a very bad way, but I have no
>> idea what I'm doing wrong!?

>
>
> I have moved the calculation of VERSION to a module _Version.py which
> contains a function Version(), which is imported by __init__.py and
> _C.py. That works and is much cleaner, but I would still be happy if
> somebody could explain we, what's going wrong.


Not an explanation, but even developers on the Python core can't always follow
the twists and turns the import logic can go through when importing a package:

http://sourceforge.net/tracker/index...70&atid=105470

Cheers,
Nick.
 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      11-22-2004
Achim Domma (Procoders) wrote:
> Hi,
>
> in my __init__.py of module A.B if have something like:
> ....
>
> import A.B
> if A.B.VERSION[0]==5: <- Error
> do_something()
>
> I get the error:
> AttributeError: 'module' object has no attribute 'B'


Do you have a file named "A.B.py"? -- such names won't work.
If "A" is a package (directory), does it have a file "__init__.py"
and a file "B.py"? If so, I don't have any more clues for you.

--Scott David Daniels

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem(s) with import from parent dir: "from ../brave.py import sir_robin" per9000 Python 7 02-27-2006 06:36 PM
to use import java.lang.* or import java.lang.Math or none at all? JPractitioner Java 13 02-24-2006 08:48 PM
XML Schema question - does "import" import elements? Vitali Gontsharuk XML 2 08-25-2005 07:33 PM
Problem with import "from omniORB import CORBA, PortableServer" Stefan Seefeld Python 3 04-11-2005 08:54 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57