Import problem
Steve Holden
steve at holdenweb.com
Mon Nov 22 09:07:28 EST 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 at dellboy ~
$ cat pkg/__init__.py
#!/usr/bin/python
objv = "2.3.45"
VERSION = tuple([int(x) for x in objv.split(".")])
del objv
sholden at 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
More information about the Python-list
mailing list