Skip to main content
 首页 » 编程设计

python之如何在谷歌云上安装 ortools 库

2024年11月24日8softidea

我正在使用命令 pip install -t lib/ortools ,库 ortools 安装到 lib/ 文件夹。但是,当我在 Google App Engine 上部署包含该库的 Flask 项目时,出现以下错误:

(/base/alloc/tmpfs/dynamic_runtimes/python27g/931d17f05408b3ef/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:263) 
Traceback (most recent call last): 
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/931d17f05408b3ef/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle 
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) 
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/931d17f05408b3ef/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler 
    handler, path, err = LoadObject(self._handler) 
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/931d17f05408b3ef/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject 
    obj = __import__(path[0]) 
  File "/base/data/home/apps/b~cidy-1539116366694/20190316t002011.416796594015344313/main.py", line 5, in <module> 
    from ortools.constraint_solver import pywrapcp 
  File "/base/data/home/apps/b~cidy-1539116366694/20190316t002011.416796594015344313/lib/ortools/constraint_solver/pywrapcp.py", line 17, in <module> 
    _pywrapcp = swig_import_helper() 
  File "/base/data/home/apps/b~cidy-1539116366694/20190316t002011.416796594015344313/lib/ortools/constraint_solver/pywrapcp.py", line 16, in swig_import_helper 
    return importlib.import_module('_pywrapcp') 
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/931d17f05408b3ef/python27/python27_dist/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
ImportError: No module named _pywrapcp 

请您参考如下方法:

第一代(Python 2.7)标准环境沙箱要求非常严格。在这方面特别相关的是 Pure Python一:

All code for the Python runtime environment must be pure Python, and not include any C extensions or other code that must be compiled.

OR-tools 无法满足此要求,因为它们需要(特定于平台的)编译。来自 their installation page :

Note: you can build OR-Tools suite from source for any supported platform only from that platform. OR-Tools Makefile does not support cross-compiling for any supported platform.

您可能仍然可以在 GAE 上使用它们:

  • 在第二代标准环境中(Python 3.7,更宽松的限制)——但我不确定是否支持 pip 驱动的包构建以及是否提供了它所需的所有工具,YMMV
  • 在灵活的环境中,最有可能使用 custom-built runtime它甚至允许您添加非 Python 依赖项 - 构建 ortools 可能需要的额外库和工具。