Azure函数Python Vx -使用ORM时出现ModuleNotFound异常

oaxa6hgo  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(109)

背景

我正在使用Azure Functions(我已经尝试了v1和v2)来协调一组在HTTP上触发的脚本。这些脚本将从API中提取数据,转换,然后使用Pony ORM上传到PostgreSQL数据库。该脚本使用Python 3.8在本地工作,我使用Az Functions的唯一原因是以下原因:
1.函数本身就支持这些脚本的HTTP触发器,而不会在我这端进行任何开销操作。这可以集成到下游解决方案中,以允许客户端触发数据刷新。
1.可以使用Azure门户调整环境变量,使非技术客户端更容易调整转换,而无需进入系统。

问题

当我尝试将我的脚本移动到Azure函数中时,我无法在本地运行此脚本,并且面临Pony的ModuleNotFound异常。我曾尝试使用其他ORM(如PeeWee),但遇到了同样的问题。

我尝试的解决方案

我已验证requirements.txt文件是否包含所有必需的软件包。我还手动安装了这些软件包,并验证它们是否出现在venv文件夹中。我已尝试将数据库代码移动到帮助文件中(在AzFunction之外)因为出于某种原因,我认为这可能会起作用,但无济于事。我将尝试Django下一个,但我很犹豫,因为我不确定它会如何影响性能(这将需要相当多的重构,如果我能让Pony工作就更容易了)。从Microsoft Excel,我知道这可能会更好地工作,因为它涉及到Linux轮和支持Linux的相应包(因为这是Azure Functions运行的)。

输出

当我在本地运行函数时,我收到这个错误:

*  Executing task: .venv\Scripts\python -m pip install -r requirements.txt 

Requirement already satisfied: azure-functions in c:\users\...
Requirement already satisfied: pony in c:\{pwd}\.venv\lib\site-packages (from -r requirements.txt (line 6)) (0.7.17)
Requirement already satisfied: ratelimit in c:\{pwd}\.venv\lib\site-packages (from -r requirements.txt (line 7)) (2.2.1)
Requirement already satisfied: requests in c:\{pwd}\.venv\lib\site-packages (from -r requirements.txt (line 8)) (2.31.0)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\{pwd}\.venv\lib\site-packages (from requests->-r requirements.txt (line 8)) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\{pwd}\.venv\lib\site-packages (from requests->-r requirements.txt (line 8)) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\{pwd}\.venv\lib\site-packages (from requests->-r requirements.txt (line 8)) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in c:\{pwd}\.venv\lib\site-packages (from requests->-r requirements.txt (line 8)) (2023.7.22)

[notice] A new release of pip available: 22.3 -> 23.3.1
[notice] To update, run: C:\{pwd}\.venv\Scripts\python.exe -m pip install --upgrade pip
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: .venv\Scripts\activate ; func host start 

Found Python version 3.8.10 (python3).

Azure Functions Core Tools
Core Tools Version:       4.0.5455 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.27.5.21554

[2023-11-12T02:04:03.820Z] Customer packages not in sys path. This should never happen! 
[2023-11-12T02:04:06.309Z] Error in index_function_app. Sys Path: {$PATH}
[2023-11-12T02:04:06.334Z] Worker failed to index functions
[2023-11-12T02:04:06.344Z] Result: Failure
Exception: ModuleNotFoundError: No module named 'ratelimit'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
Stack:   File "C:\Users\Nathan Verghis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 345, in _handle__functions_metadata_request
    fx_metadata_results = self.index_functions(function_path)
  File "C:\Users\Nathan Verghis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 617, in index_functions
    indexed_functions = loader.index_function_app(function_path)
  File "C:\Users\Nathan Verghis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 48, in call
    raise extend_exception_message(e, message)
  File "C:\Users\Nathan Verghis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 44, in call
    return func(*args, **kwargs)
  File "C:\Users\Nathan Verghis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\loader.py", line 214, in index_function_app
    imported_module = importlib.import_module(module_name)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\{pwd}\function_app.py", line 4, in <module>
    from ratelimit import limits, sleep_and_retry
.
[2023-11-12T02:04:07.068Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
For detailed output, run func with --verbose flag.

字符串
我会保持这一更新,如果它的作品。如果有人知道一种方法来实现这个功能没有功能(同时保留上述好处),或如何让ORM正确安装在功能,请让我知道。谢谢

--更新--

我最初是在Windows上开发的。切换到Mac似乎完全避免了ModuleNotFoundError。我在最初部署的代码中发现了一些错误,在修复它们之后,代码也能够在Azure上工作。将调试后的代码带回Windows,仍然没有本地的运气。在这一点上,我只是好奇是什么导致了这个问题。

wvt8vs2t

wvt8vs2t1#

我尝试了下面的函数代码与**pony-orm**包连接到PostgreSQL,它成功地工作如下:

函数Python - v1,Python版本3.10
init.py code:-

import os
import json
import azure.functions as func
from pony.orm import Database, Required, db_session, select

# Define your Pony ORM entities here
db = Database()

# Define your entity model (example)
class Product(db.Entity):
    name = Required(str)
    price = Required(float)

# Initialize Pony ORM database connection
def initialize_database():
    db.bind(provider='postgres', user='siliconuser', password='xxxx',
            host='xxxxnpostgresql.postgres.database.azure.com', database='postgres')
    db.generate_mapping(create_tables=True)

# Call the initialize function outside of a db_session
initialize_database()

# HTTP trigger function
def main(req: func.HttpRequest) -> func.HttpResponse:
    with db_session:
        # Example: Inserting data into the database
        new_product = Product(name='Sample Product', price=20.0)

    with db_session:
        products = select(p for p in Product)[:]
        products_list = [{'name': p.name, 'price': p.price} for p in products]

    return func.HttpResponse(json.dumps(products_list), mimetype="application/json")

字符串

My requirements.txt :-

对于pony包与postgresql psycopg 2-二进制包也是必需的。

azure-functions

pony

psycopg2-binary

输出:-

在本地加载Azure Functions并通过虚拟环境安装程序包时,**Click on fn + f5 or Just f5这将初始化该函数,使其沿着venv**程序包一起运行。请确保系统中有Azure Functions Core tools installed
x1c 0d1x的数据



在部署Function而不是使用Azure Functions Extension时,* 我使用了Azure Functions Core tools命令,如下所示:-*

az login
az account set --subscription "SID Subscription"
func azure functionapp publish siliconfunc89

输出:-


HttpTrigger已 * 部署并成功运行 * 参考如下:-


  • Azure Function应用程序Python版本- 3.10,包含Premium Function计划:-*

而使用Premium或Dedicated Function计划导入大型软件包更有效。

注意-我正在使用Azure PostgreSQL。
此外,在您的错误代码-python Customer packages not in sys path. This should never happen! [2023-11-12T02:04:06.309Z] Error in index_function_app. Sys Path: {$PATH} [2023-11-12T02:04:06.334Z] Worker failed to index functions中,当部署时未将python包添加到正确的文件夹中时会发生错误。为了解决此问题并获得更多有关此问题的见解,请参阅Abdul Niyas P M和Pravallika的此SO线程答案。请确保将Azure Functions Core工具降级为python 4.0.534

  • 我的Azure函数核心工具版本:-*
4.0.5148


您也可以手动初始化 * 虚拟环境,然后安装包,使用func start命令运行您的函数:-

py -m venv .venv
.venv\scripts\activate

相关问题