隐藏 stdlib 包名称的子包中的绝对导入失败 [英] Absolute import failing in subpackage that shadows a stdlib package name

查看:12
本文介绍了隐藏 stdlib 包名称的子包中的绝对导入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个与标准库包(日志")同名的子包,我希望它能够绝对导入标准包,无论我如何运行它,但是当我'在父包中.

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.

它真的看起来像是一个错误,或者是新的绝对导入"支持(Python 2.5 中的新功能)的未记录行为.用 2.5 和 2.6 试过.

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.

包装布局:

foo/
    __init__.py
    logging/
        __init__.py

foo/__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

foo/logging/__init__.py 中,我们要导入 stdlib 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

注意:包含foo的文件夹在sys.path中.

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

当从外部/上面foo导入时,输出如预期:

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'>

所以子包中的绝对导入会根据需要找到stdlib包.

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

但是当我们在 foo 文件夹中时,它的行为会有所不同:

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'>

"sub, name" 的双重输出表明我自己的名为 "logging" 的子包正在第二次导入自身,并且它没有找到 stdlib "logging" 包 即使 "absolute_import" 已启用.

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.

任何想法发生了什么,这是一个错误(我的还是 Python 的),或者这种行为是否实际上是由某些文档暗示的?

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?

gahooa 的回答清楚地表明了问题所在.此处显示了一个粗略的解决方法,可以证明就是这样:

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'>

推荐答案

sys.path[0] 默认是 '',意思是当前目录".因此,如果您所在的目录中包含 logging,则将首先选择该目录.

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.

我最近遇到了这个问题,直到我意识到我实际上坐在那个目录中并且 sys.path 在查看标准库之前首先选择了我的当前目录.

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.

这篇关于隐藏 stdlib 包名称的子包中的绝对导入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆