【发布时间】:2012-10-25 09:30:56
【问题描述】:
可能重复:
How to access a standard-library module in Python when there is a local module with the same name?
我使用的是 Python 2.6。
我只在我的应用程序中使用绝对导入。现在我有了这个:
myapp
|
-- myscript
-- json
|
-- anotherscript.py
在我的脚本中,我有:
import json
import myapp.json.anotherscript
由于Python相对导入机制,import json并没有按照我的意愿导入内置库,而是将我自定义的json包导入到当前命名空间中。
在这种情况下,有没有办法在 Python 中禁用相对导入,或者至少有一种技巧可以避免这种情况?否则,我将不得不将我的包重命名为其他没有 jsonutils 意义的东西。
提前致谢。
【问题讨论】:
-
为什么要避开
from . import json?引入该语法是为了解决您的问题!无论如何,作为一项规则,永远不要将模块命名为内置。