Import problem

  • Thread starter Achim Domma (Procoders)
  • Start date
A

Achim Domma (Procoders)

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
 
A

Achim Domma (Procoders)

Achim said:
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
 
S

Steve Holden

Achim said:
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
 
N

Nick Coghlan

Achim said:
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.php?func=detail&aid=992389&group_id=5470&atid=105470

Cheers,
Nick.
 
S

Scott David Daniels

Achim said:
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
(e-mail address removed)
 

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

Similar Threads


Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,694
Latest member
digiadvancemarketing

Latest Threads

Top