0

当我尝试使用 __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 类相关联吗?

4

1 回答 1

0

tica/sources/processors.py文件似乎是您的应用程序的一部分。如果是这样,它显式依赖于pip包,它似乎不是标准 python 库的一部分,也不包含在 GAE Runtime-Provided Libraries中。这意味着它需要.

您想要动态加载的所有包/模块及其所有依赖项可能同样需要被供应。

在尝试执行动态加载的代码之前,我还会添加更多的完整性检查。至少抓和处理ImportError

旁注:以防万一task_handler.startProcessing(parameters)可能需要太长时间并导致DeadlineExceededError请求post,您可能希望将执行委托给任务队列或后端执行模块。

于 2015-07-31T15:08:11.277 回答