当我尝试使用 __import__ 实现某种动态类加载时出现以下错误...:
No module named pip._vendor.requests.status_codes
Traceback (most recent call last):
File"/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~ticatestdev/2.386059027148196174/tica/tasks.py", line 36, in post
m = __import__(handler['module'], fromlist=[handler['name']])
File "/base/data/home/apps/s~ticatestdev/2.386059027148196174/tica/sources/processors.py", line 18, in <module>
from pip._vendor.requests.status_codes import codes
ImportError: No module named pip._vendor.requests.status_codes
...或 importlib.import_module :
No module named pip._vendor.requests.status_codes
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~ticatestdev/2.386058813285719320/tica/tasks.py", line 36, in post
m = importlib.import_module(handler['module'])
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/base/data/home/apps/s~ticatestdev/2.386058813285719320/tica/sources/processors.py", line 18, in <module>
from pip._vendor.requests.status_codes import codes
ImportError: No module named pip._vendor.requests.status_codes
我当然导入 importlib。
我在 Google AppEngine 服务器上部署我的程序来测试它(我有一个生产 URL 和一个开发 URL)。
这是代码:
import importlib
import json
import logging
class Process(handlers.BaseHandler):
def post(self):
parameters = json.loads(self.request.get('parameters'))
if parameters:
if 'handler' in parameters:
handler = parameters['handler']
m = __import__(handler['module'], fromlist=[handler['name']])
#m = importlib.import_module(handler['module'])
task_handler = getattr(m, handler['name'])(parameters)
#task_handler = getattr(m, handler['name'])(parameters)
task_handler.startProcessing(parameters)
else:
logging.error("python.tasks.Process(): No handler information provided!")
else:
logging.error("python.tasks.Process(): No parameters provided!")
和参数内容:
{"dictionary_list": [...], "handler": {"name": "SourceProcessingHandler", "module": "sources.processors"}, "command": "process", "fsm": {}, "language_code": "ja"}
我在谷歌上找不到类似的错误,
我现在想知道我是否没有浪费时间试图用这个复杂的解决方案超越自己......
可能我应该使用一个简单的字典将名称与 python 类相关联吗?