RickyA RickyA - 8 months ago 59
Python Question

Force Python import to be absolute (ignore local package directory)

Is there a way to force an import to be absolute instead of relative?

I am "overriding" the Python standard library

module, so in my projects I always use the correct encoder and parameters:

: (mark this filename)

import json as pyjson

class ComplexEncoder(pyjson.JSONEncoder):
def default(self, obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
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)

When I import this file I get the dreaded
AttributeError: 'module' object has no attribute 'JSONEncoder'
confirms my suspicion that
import json as pyjson
from the local package instead of from the Python standard library.

Is there a way to force the import to be absolute, so the local directory is ignored?


If your "json" module is inside a package, then this will fix it:

from __future__ import absolute_import

With that __future__ statement, imports will be absolute be default - i.e. it will only look for a top-level module or file called json.

If you need to import your local one, you can either do import foo.json, or you can explictly ask for a relative import using from . import json or from .json import dumps.

(I am assuming you're using Python 2).