issue on internal import in a package

Frank Millman frank at
Sun Feb 27 15:05:24 CET 2011

"人言落日是天涯,望极天涯不见家" < at> wrote in message 
news:9529d52b-01b2-402c-a0a0-1e92400380b7 at
> 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."
>> HTH
>> 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.

I think that PEP 366 addresses your question -

I came across this while investigating a separate problem. It looks a bit 
complicated, and I don't have such a requirement, so I did not look at it 
too closely.

Have a read and see if it solves your problem. If not post a follow-up 
message here and see if someone else can offer more information.


More information about the Python-list mailing list