Import error when trying to access sdk datastore admin
I'm getting a "ImportError: No module named simplejson" when trying to access datastore admin on the development server.
After the first time I got this error I installed the simplejson package from slackbuilds (i'm on Slackware). It seems to be working normally, I could import the simplejson module with no errors on the python console.
But the import error persists when I try to access datastore admin, even after installing the module. Restarting the development server didn't do anything.
What could be wrong?
Edit: here's the stacktrace:
Traceback (most recent call last):
File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 3245, in _HandleReques开发者_运维百科t self._Dispatch(dispatcher, self.rfile, outfile, env_dict) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 3186, in _Dispatch base_env_dict=env_dict) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 531, in Dispatch base_env_dict=base_env_dict) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 2410, in Dispatch self._module_dict) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 2320, in ExecuteCGI reset_modules = exec_script(handler_path, cgi_path, hook) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 2216, in ExecuteOrImportScript exec module_code in script_module.dict File "/home/lwlun/AppEngine/google_appengine/google/appengine/ext/datastore_admin/main.py", line 30, in from google.appengine.ext.datastore_admin import copy_handler File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1945, in load_module return self.FindAndLoadModule(submodule, fullname, search_path) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1847, in FindAndLoadModule description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1798, in LoadModuleRestricted description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/ext/datastore_admin/copy_handler.py", line 40, in from google.appengine.ext.datastore_admin import utils File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1945, in load_module return self.FindAndLoadModule(submodule, fullname, search_path) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1847, in FindAndLoadModule description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1798, in LoadModuleRestricted description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/ext/datastore_admin/utils.py", line 35, in from google.appengine.ext.mapreduce import control File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1945, in load_module return self.FindAndLoadModule(submodule, fullname, search_path) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1847, in FindAndLoadModule description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1798, in LoadModuleRestricted description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/ext/mapreduce/control.py", line 26, in from google.appengine.ext.mapreduce import handlers File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1945, in load_module return self.FindAndLoadModule(submodule, fullname, search_path) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1847, in FindAndLoadModule description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1295, in Decorate return func(self, *args, **kwargs) File "/home/lwlun/AppEngine/google_appengine/google/appengine/tools/dev_appserver.py", line 1798, in LoadModuleRestricted description) File "/home/lwlun/AppEngine/google_appengine/google/appengine/ext/mapreduce/handlers.py", line 26, in import simplejson ImportError: No module named simplejson
Use from django.utils import simplejson
.
dev_appserver mimics the production environment. Nothing you install at the system level will be available for import to your scripts, to avoid imports working fine in your testing and then failing spectacularly when you deploy. However, you don't need to bundle this particular module because it's already available in the django package installed on production (and with the SDK).
In general, you do need to include external libraries in your application directory, although you can symlink them from their installed locations and they'll generally work fine (and will be deployed to production when you deploy).
I found a solution in a similar question: ImportError when using Google App Engine
Copying the module to the app directory worked, but it kinda feels like a cheap workaround.
If it in app engine SDK's lib, e.g on Mac it's
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/
Copy it over to app's dir would work. It's better than pip install and then copy over, which may contain c-extension. The modules in SDK's lib path (at least simplejson) seems to have been modified to work with app engine's needs.
精彩评论