インポートを相対ではなく絶対にする方法はありますか?
私は Python 標準ライブラリjson
モジュールを「オーバーライド」しているので、プロジェクトでは常に正しいエンコーダーとパラメーターを使用します。
project/foo/json.py
: (このファイル名をマーク)
import json as pyjson
class ComplexEncoder(pyjson.JSONEncoder):
def default(self, obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
else:
if type(obj) == file:
return "filestream"
raise TypeError, 'Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj))
def dumps(data):
return pyjson.dumps(data, cls=ComplexEncoder, check_circular=False, separators=(',', ':'), ensure_ascii=False)
def loads(data):
return pyjson.loads(data)
このファイルをインポートすると、恐ろしいAttributeError: 'module' object has no attribute 'JSONEncoder'
. Aは、Python 標準ライブラリからではなく、ローカル パッケージからインポートするprint(pyjson.__file__)
という私の疑いを裏付けています。import json as pyjson
json
ローカル ディレクトリが無視されるように、インポートを強制的に絶対にする方法はありますか?