0

I'm using wadofstuff's serializer https://pypi.python.org/pypi/wadofstuff-django-serializers on django 1.5. However, it uses simplejson which is now deprecated so I have to go directly into the library file wadofstuff/django/serializers/json.py and change simplejson into json. The problem is that I cannot import json because the library file is named json.py as well, so it tries to load itself. I don't want to change the file name because other developers in my team will definitely kill me. Is there any way to import json from absolute path?

Thank you.

1
  • 1
    It really seems like you ought to file a bug, given that it was still being updated in 2012. And then implement the clean fix (rename the file to something other than json, and do the traditional try-import-json-except-import-simplejson fix) and submit it as a patch. Commented Mar 16, 2013 at 7:58

4 Answers 4

2

The simplest, and probably best, fix for this (in Python 2.x) is to not have a module that shadows the name of a top-level stdlib/site-packages module.

In other words, rename json.py to something else. Then you can just import json from within the renamed file (or, better, try that, and on ImportError fall back to simplejson, so you don't break 2.5 compatibility). Then just change whatever code was importing serializers.json to import the new thing.

You should be able to file a bug against the wadostuff package, and submit your patch, and it may make it into version 1.2. (There seems to be an update about once/year or so, and it doesn't seem too unreasonable to finally get up to speed with Python 2.6 and Django 1.5 in 2013…)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion. I'll do that.
0

Another way is to remove the current directory from the search path. Assuming "" (denotes the current directory) is at the beginning of sys.path (the default):

sys.path.remove("")
import json
sys.path.insert(0, "")

1 Comment

I appreciate every hack like that for the smart thinking. But just for others who come here and read this approach: Never ever do this. You don't have to. There are a lot of other ways and every one of them is better than changing your system path.
0

You can put this line on top of json.py:

from __future__ import absolute_import

This will tell Python that, while importing this module, it should look for modules only on sys.path and not relatively to it (see also http://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports).

Edit:

Also note that the wadofstuff's module doesn't actually import simplejson directly: from django.utils import simplejson. Django code will use the stdlib version by default if you don't have simplejson installed.

3 Comments

That almost works, except that json.py is already named json, and import json will still just return itself. If the problem were another module in the package that wanted to import the stdlib module, this would be the answer.
@abnarnert You're wrongly assuming that the file 'json.py' is on sys.path. The full qualified name of that module is wadofstuff.django.serializers.json, so it does not shadow the stdlib in anyway.
The json.py he's trying to do the import from may not be on sys.path (hopefully it isn't), but the one he's try to import from within it definitely is. If it's being imported from outside the package as wadofstuff.django.serializers.json it'll work with absolute_import; if it's being imported through traditional import as import json from a sibling, it won't. If you've already checked and seen that it's not being imported the latter way anywhere, then I apologize, your answer is correct.
-1

1, try change python's import-path:

sys.path.insert(0, "/path/to/your/json.py/dir")
import json

2, try add _ _ init _ _.py file to all dirs to your json.py, then you can use this

import a.b.c.json as myjson

3, if you dont want change any bit, try something deep with

__import__()

1 Comment

1) would actually make things worse and 2) is already done in the library the OP writes about. And 3) is not really helpful?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.