Reputation: 746
How do I explicitly tell Google App Engine (python) to import json from the python standard libraries?
Due to a poorly named file (that I can NOT change or rename at this time) I am having trouble importing json.
There is a json.py in the same directory as the file that I am working on. When I try:
import json
it imports the file in the same directory.
Is there a way I can do something along the lines of:
from ../ import json
to import the Native JSON library?
EDIT:
I have now even tried renaming the offending file and replacing all uses of this file. But I'm still not able to import the json standard lib through GAE.
Attached is the error log:
File "/Users/admin/Blah/dataaccess.py", line 13, in <module>
from classes import ZenDesk
File "/Users/admin/Blah/classes/ZenDesk.py", line 10, in <module>
import json
File "/Users/admin/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 892, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named classes.json
Please Help google app engine should be looking for the standard library and not in the subdirectory classes
Upvotes: 2
Views: 582
Reputation: 174624
Try this, although I'm not sure if it will work on GAE since its a PaaS platform:
import os
import sys
from distutils.sysconfig import get_python_lib
sys.path.insert(0,os.path.dirname(get_python_lib()))
import json
sys.path = sys.path[1:]
Upvotes: 0
Reputation: 17168
If you can't rename the module for some reason, you have to mess around with sys.path
to put your standard lib in front of the current module. Make sure to fix it again after you do your import, though...
import sys
sys.path = [r'C:\Python27\Lib'] + sys.path
import json
sys.path = sys.path[1:]
Alternately, I'm pretty sure the imp module has functionality for this.
Let me be clear, though: it would be much better (as others have said) to just rename the module in the project directory (e.g. to my_json.py
). You're not going to have to change that many dependencies.
Some Edits:
If you cannot find an actual path, absolute or otherwise, to the standard lib, you have two further options besides renaming the thing. You can move the file you're working on to a different level in your package (so that it can't see the local json.py
). For instance, like this:
/package
__init__.py
your_file.py # "import json" should now hit the std lib.
# To import the local copy, use "from app import json"
app/
__init__.py
json.py
other_thing.py # contains "import json" to get the local copy
If you don't want to move your file, then you can fake it by adding an extra file that does nothing but import the real json module:
/package
__init__.py
json_importer.py # contains nothing but "import json"
app/
__init__.py
json.py
your_file.py # "from package.json_importer import json"
other_thing.py # contains "import json" to get the local copy
Upvotes: 1
Reputation: 869
Try this perhaps?
import imp
json = imp.load_source('module.name', '/path/to/built-in/json/file.py')
Upvotes: 0