__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"}
Googleで同様のエラーを見つけることができませんでした、
この複雑なソリューションで自分の裏をかくために時間を無駄にしていないのではないかと今は思います...
名前をpythonクラスに関連付ける簡単な辞書を使用する必要がありますか?