无意中覆盖 Python 2.7 标准模块 - 如何防止?

Unintentionally overwriting Python 2.7 standard module - how to prevent?

我想在 Python 的日志模块中添加一些东西,所以我在包名 "my" 下创建了一个单独的模块,它位于项目目录中(这是sys.path)。我还有另一个模块,它位于同一个 space:

my
my.logging
my.something

现在我想在 my.something 中记录内容并向文件添加 import logging - 但似乎 Python 加载 my.logging 而不是标准模块。

阅读 Python 文档(第 6.1.2 节。模块搜索路径)时,我认为我是安全的:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path.

所以 "standard module" 不是 "built-in module"?我想,我必须将 my 模块移出当前目录 - ?其他可能性?

当它说 "built-in module" 时,该行文档指的是实际直接编译到 Python 可执行文件中的模块。您可以查看 sys.builtin_module_names.

中的那些

您需要做的是关闭隐式相对导入,这可以使用 absolute_import future 语句在每个文件的基础上完成:

from __future__ import absolute_import