开发者

Absolute import failing in subpackage that shadows a stdlib package name

Basically I have a subpackage with the same name as a standard library package ("logging") and I'd like it to be able to absolute-import the standard one no matter how I run it, but this fails when I'm in the parent package.

It really looks like either a bug, or an undocumented behaviour of the new "absolute import" support (new as of Python 2.5). Tried with 2.5 and 2.6.

Package layout:

foo/
    __init__.py
    logging/
        __init__.py

In foo/__init__.py we import our own logging subpackage:

from __future__ import absolute_import
from . import logging as rel_logging
print 'top, relative:', rel_logging

In foo/logging/__init__.py we want to import the stdlib logging package:

from __future__ import absolute_import
print 'sub, name:', __name__

import logging as abs_logging
print 'sub, absolute:', abs_logging

Note: The folder containing foo is in sys.path.


When imported from outside/above foo, the output is as expected:

c:\> python -c "import foo"
sub, name: foo.logging
sub, absolute: <module 'logging' from 'c:\python26\lib\logging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'foo\logging\__init__.pyc'>

So the absolute import in the subpackage finds the stdlib package as desired.

But when we're inside the foo folder, it behaves differently:

c:\foo>\python25\python -c "import foo"
sub, name: foo.logging
sub, name: logging
sub, absolute: <module 'logging' from 'logging\__init__.pyc'>
sub, absolute: <module 'logging' from 'logging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'c:\foo\logging\__init__.pyc'>

The double output for "sub, name" shows that my own subpackage called "logging" is importing itself a second time, and it does not find the stdlib "logging" package even though "absolute_import" is enabled.

The use case is that I'd like to be able to work with, test, etc, this package regardless of what the current directory is. Changing the name from "logging" to something else would be a workaround, but not a desirable one, and in any case this behaviour doesn't seem to fit with the description of how absol开发者_如何学编程ute imports should work.

Any ideas what is going on, whether this is a bug (mine or Python's), or whether this behaviour is in fact implied by some documentation?

Edit: the answer by gahooa shows clearly what the problem is. A crude work-around that proves that's it is shown here:

c:\foo>python -c "import sys; del sys.path[0]; import foo"
sub, name: foo.logging
sub, absolute: <module 'logging' from 'c:\python26\lib\logging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'c:\foo\logging\__init__.pyc'>


sys.path[0] is by default '', which means "current directory". So if you are sitting in a directory with logging in it, that will be chosen first.

I ran into this recently, until I realized that I was actually sitting in that directory and that sys.path was picking up my current directory FIRST, before looking in the standard library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜