issue on internal import in a package

人言落日是天涯,望极天涯不见家 kelvin.you at gmail.com
Sun Feb 27 08:50:30 EST 2011


On Feb 27, 9:38 pm, "Frank Millman" <fr... at chagford.com> wrote:
> "人言落日是天涯,望极天涯不见家" <kelvin.... at gmail.com> wrote in message
>
> news:fa94323b-d859-4599-b236-c78a22b3d4dd at t19g2000prd.googlegroups.com...
>
> > On Feb 27, 9:22 pm, "Frank Millman" <fr... at chagford.com> wrote:
>
> > This behavior is by design or just a bug for Python3.x ?
>
> Definitely by design.
>
> Have a look at PEP 328 -http://www.python.org/dev/peps/pep-0328/
>
> "In Python 2.4 and earlier, if you're reading a module located inside a
> package, it is not clear whether
> import foo
> refers to a top-level module or to another module inside the package. As
> Python's library expands, more and more existing package internal modules
> suddenly shadow standard library modules by accident. It's a particularly
> difficult problem inside packages because there's no way to specify which
> module is meant. To resolve the ambiguity, it is proposed that foo will
> always be a module or package reachable from sys.path. This is called an
> absolute import."
>
> HTH
>
> Frank

Yes, it's okay with the change in a.py with below line:
from . import b

But another issue occurred if I want to run the a.py separately.
$ cd module
$ python a.py
Traceback (most recent call last):
  File "a.py", line 1, in <module>
    from . import b
ValueError: Attempted relative import in non-package

Does that mean the relative import only allowed in the package.
And it cannot be run as __main__ program unless I change the relative
import back to absolute import?
I think this behavior is strange and difficult to use.


Doesn't



More information about the Python-list mailing list