Connecting Tech Pros Worldwide Forums | Help | Site Map

Import problem

Achim Domma (Procoders)
Guest
 
Posts: n/a
#1: Jul 18 '05
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

Achim Domma (Procoders)
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Import problem


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

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
Steve Holden
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Import problem


Achim Domma (Procoders) wrote:
[color=blue]
> Achim Domma (Procoders) wrote:
>[color=green]
>> Seems like I'm mixing up something in a very bad way, but I have no
>> idea what I'm doing wrong!?[/color]
>
>
> 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[/color]

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
Nick Coghlan
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Import problem


Achim Domma (Procoders) wrote:[color=blue]
> Achim Domma (Procoders) wrote:
>[color=green]
>> Seems like I'm mixing up something in a very bad way, but I have no
>> idea what I'm doing wrong!?[/color]
>
>
> 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.[/color]

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.
Scott David Daniels
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Import problem


Achim Domma (Procoders) wrote:[color=blue]
> 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'[/color]

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
Scott.Daniels@Acm.Org
Closed Thread