2012-11-05 61 views
6

可能重複:
How to access a standard-library module in Python when there is a local module with the same name?的Python:禁用相對進口

我使用Python 2.6。

我只在我的應用程序中使用絕對導入。現在我有這樣的:

myapp 
    | 
    -- myscript 
    -- json 
     | 
     -- anotherscript.py 

在的MyScript,我有:

import json 
import myapp.json.anotherscript 

因爲Python相對進口機制,進口JSON不會導入內置庫,我想,但我自定義的JSON打包進當前的命名空間。

有沒有辦法在Python中禁用相對導入,或至少有一種黑客來避免它在這種情況下?否則,我將不得不將我的軟件包重命名爲其他沒有jsonutils那麼有意義的東西。

在此先感謝。

+3

。導入json'?引入了這個語法來解決你的問題!無論如何,作爲一項規則,永遠不要將模塊命名爲內置模塊。 – Bakuriu

回答

15
from __future__ import absolute_import 

爲什麼你想避免'從描述中PEP-328

+0

似乎沒有用Python 2.7 – sorin

+0

@sorin它是如何失敗的?它對我來說工作得很好 – silviot