在子包中绝对导入失败,影响stdlib包名称 [英] Absolute import failing in subpackage that shadows a stdlib package name

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

问题描述

基本上我有一个与标准库包相同名称的子包(logging),我希望它能够绝对导入标准库,无论我如何运行它,但是当我运行时它会失败在父包中。

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.

它看起来像是一个bug,或者是新的绝对导入支持的无证行为(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

In 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

In 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

注意:包含<$ c $的文件夹c> 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:\python26\lib\logging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'foo\logging\__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>\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'>

sub,name的双输出显示我自己的名为logging的子包正在自行导入第二次,它找不到stdliblogging包,即使启用了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:\python26\lib\logging\__init__.pyc'>
top, relative: <module 'foo.logging' from 'c:\foo\logging\__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天全站免登陆