Google AppEngine dynamic loading, ImportError: No module named pip._vendor.requests.status_codes

Google AppEngine dynamic loading, ImportError: No module named pip._vendor.requests.status_codes

当我尝试使用 __import__ 实现某种动态 class 加载时出现以下错误 ...:[=​​14=]

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 classes?

tica/sources/processors.py 文件似乎是您的应用程序的一部分。如果是这样,它明确依赖于 pip 包,它似乎不是标准 python 库的一部分,也不包含在 GAE Runtime-Provided Libraries. This means it would need to be vendored in.

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

在尝试执行动态加载的代码之前,我还会添加更多的健全性检查。至少捕获和处理 ImportError.

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