issue on internal import in a package

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

On Feb 27, 9:38 pm, "Frank Millman" <fr... at> wrote:
> "人言落日是天涯,望极天涯不见家" <kelvin.... at> wrote in message
> news:fa94323b-d859-4599-b236-c78a22b3d4dd at
> > On Feb 27, 9:22 pm, "Frank Millman" <fr... at> wrote:
> > This behavior is by design or just a bug for Python3.x ?
> Definitely by design.
> Have a look at PEP 328 -
> "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."
> Frank

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

But another issue occurred if I want to run the separately.
$ cd module
$ python
Traceback (most recent call last):
  File "", 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.


More information about the Python-list mailing list