Azure 函数中的 Google.OR-Tools

问题描述 投票:0回答:4

我最近一直在使用 Python 中的 OR-Tools,并尝试将我的代码移动到 Python 中的 Azure 函数。有谁知道是否支持?

我尝试过并不断遇到无法导入 OR-Tools 模块(特别是来自 ortools.constraint_solver 的 pywrapcp 和routing_enums_pb2)的问题。我已确认我的 Python 版本是 64 位。

根据 OR-Tools 发行版,它支持 Linux,但是我一直在阅读,似乎这可能与需要安装的 C++ 可再发行组件有关。这是我现在完全超出我的能力范围的地方,可以使用一些指针。这在Python函数中可能吗?

编辑

按照下面 Bowman Zhu 的评论 - 我已经检查了 ORTools 是否存在于我的环境中 - 请参阅下面的片段。该脚本在我尝试导入 pywrapcp 的位置失败。

from ortools.constraint_solver import pywrapcp

但它肯定是存在的。有什么建议吗?

enter image description here

python azure-functions or-tools
4个回答
0
投票

如果我没记错的话,我们的 or-tools 邮件列表或 github 问题:

  • or-tools 是一个原生 Python(Python 部分只是其上的 SWIG 包装器)库,目前我们只能使用 VS 2019 构建它(因为 VS 2017 编译器崩溃了)

  • 您必须安装 VS C++ Redistributable 2019,因为 c++ 库依赖于它

  • Azure 映像还没有 VS Redistributable 2019 AFAIK

  • 当 python 解释器尝试加载本机库时,它无法找到所有依赖项,并且您会收到此错误消息...


0
投票

所以我开始工作了 - Python Azure Function 运行 OR-Tools。

在我的测试中,我可以想到您的问题的两个可能原因(我不确定您是否遇到本地问题,或部署后的问题);

  • 您使用的是本地安装的 python 解释器,而不是此处的版本
    .\.venv\Scripts\python.exe
    ,因此 pylint 无法找到依赖项的 venv 副本。
  • 您缺少在
    ortools
    中包含
    requirements.txt
    ,因此它不会在部署时安装在服务器端。

这就是导致 Azure 在部署函数时安装依赖项的原因。您不需要手动包含它们;

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions
ortools

有几点需要注意;

  • OR-Tools 是原生 C++,带有其他所有内容的包装器。
  • Python 函数仅在 Azure 中的 Linux 上运行,因此服务器端不需要 C++ 可再发行组件。
  • 在 Windows 上安装适用于 Visual Studio Code 的 Azure Functions 核心工具 将允许您在本地调试 Python Azure Function(我假设这确实需要 C++ 可再发行组件用于本地 Windows 执行)。
  • 我使用 Visual Studio 扩展“Azure Functions”进行部署。

我基本上遵循了这个指南,然后向

requirements.txt
添加了OR-Tools,并添加了一个简单的示例解决方案用于本地测试,然后部署并测试了服务器端。


0
投票

我也面临着同样的问题。

我已在我的requirements.txt 文件中包含了ortools:

azure-functions
ortools

当我部署“hello word”示例函数时,我可以看到模块安装正常:

Successfully installed absl-py-1.4.0 azure-functions-1.15.0 numpy-1.25.0 ortools-9.6.2534 protobuf-4.23.3 scipy-1.11.0

HTTP 触发器已注册正常。 但是,如果我尝试从 .py 示例函数中的 ortools 导入 pywrapcp 并再次部署,我会收到错误 未找到 HTTP 触发器。 并且部署日志中没有其他错误。

(如果我在本地运行该函数(func host start)(通过 VRP 问题解决)一切正常。)

部署到azure function时出现问题。

源代码:

import azure.functions as func
import logging
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="HttpTrigger2", auth_level=func.AuthLevel.ANONYMOUS)
def HttpTrigger2(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
    try:
        req_body = req.get_json()
    except ValueError:
        pass
    else:
        name = req_body.get('name')

if name:
    return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
    return func.HttpResponse(
         "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
         status_code=200
    )

0
投票

我面临同样的问题:

enter image description here

我的Python版本也是64位:

enter image description here

也许你正在阅读这份Google工具文档:

https://developers.google.com/optimization/install/python/windows

提示:我已经安装了你提到的C++可再发行组件,但仍然出现此错误。我检查了Azure函数的库,它没有ortools模块。

我认为答案是虚拟环境确实没有这个模块:

enter image description here

您需要运行此命令在虚拟环境中安装ortools:

pip install ortools 

然后ortools就会在虚拟环境中,导入也不再返回错误:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.