Azure 函数中的 Pyodbc

Pyodbc in Azure functions

我正在尝试使用一些 Python 代码在本地 运行 Azure 函数。我想使用 pyodbc 连接到数据库。我收到以下错误:

[2021-07-01T14:32:06.429Z] Worker failed to function id e64112a7-17de-4bd8-bb94-46ddfe91a75b.
[2021-07-01T14:32:06.429Z] Result: Failure
[2021-07-01T14:32:06.429Z] Exception: ImportError: dlopen(/Users/gonczor/trimmed/azure_functions/.venv/lib/python3.8/site-packages/pyodbc.cpython-38-darwin.so, 2): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
[2021-07-01T14:32:06.429Z]   Referenced from: /Users/gonczor/trimmed/azure_functions/.venv/lib/python3.8/site-packages/pyodbc.cpython-38-darwin.so
[2021-07-01T14:32:06.429Z]   Reason: image not found. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2021-07-01T14:32:06.429Z] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/dispatcher.py", line 302, in _handle__function_load_request
[2021-07-01T14:32:06.430Z]     func = loader.load_function(
[2021-07-01T14:32:06.430Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
[2021-07-01T14:32:06.430Z]     raise extend_exception_message(e, message)
[2021-07-01T14:32:06.430Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
[2021-07-01T14:32:06.430Z]     return func(*args, **kwargs)
[2021-07-01T14:32:06.430Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/loader.py", line 83, in load_function
[2021-07-01T14:32:06.430Z]     mod = importlib.import_module(fullmodname)
[2021-07-01T14:32:06.430Z]   File "/usr/local/Cellar/python@3.8/3.8.10/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
[2021-07-01T14:32:06.430Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2021-07-01T14:32:06.430Z]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[2021-07-01T14:32:06.430Z]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[2021-07-01T14:32:06.430Z]   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
[2021-07-01T14:32:06.430Z]   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
[2021-07-01T14:32:06.430Z]   File "<frozen importlib._bootstrap_external>", line 848, in exec_module
[2021-07-01T14:32:06.430Z]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[2021-07-01T14:32:06.430Z]   File "/Users/gonczor/trimmed/azure_functions/generate_report/__init__.py", line 2, in <module>
[2021-07-01T14:32:06.430Z]     from config import get_db_cursor
[2021-07-01T14:32:06.430Z]   File "/Users/gonczor/trimmed/azure_functions/config.py", line 3, in <module>
[2021-07-01T14:32:06.430Z]     import pyodbc
[2021-07-01T14:32:06.430Z] .

我的 requirements.txt 文件中列出了 pyodbc:

azure-functions==1.7.2
certifi==2021.5.30
chardet==4.0.0
idna==2.10
pyodbc==4.0.30
requests==2.25.1
urllib3==1.26.6

Microsoft 网站上的故障排除指南在上下文中提到了 pyodbc,其中建立了太多连接,但我只创建了其中一个。项目根目录下有一个配置文件:

import os

import pyodbc


def get_pyodbc_db_url() -> str:
    db_user = os.environ.get("DB_USER")
    db_password = os.environ.get("DB_PASSWORD")
    db_host = os.environ.get("DB_HOST")
    db_name = os.environ.get("DB_NAME")

    return 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + db_host + ';DATABASE=' + db_name + ';UID=' + db_user + ';PWD=' + db_password


def get_db_cursor() -> pyodbc.Cursor:
    # https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-ver15
    connection = pyodbc.connect(get_pyodbc_db_url())
    return connection.cursor()

原来是我少了驱动。 macos 的安装说明 can be found here.