Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

python - 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:python26liblogging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'foologging\__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>python25python -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:foologging\__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 absolute 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:python26liblogging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'c:foologging\__init__.pyc'>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...