Python Azure Function 中的 ModuleNotFoundError(在本地和 azure 云运行期间)

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

好吧,我已经搜索了有关堆栈溢出的每个主题,但没有找到合适的解决方案。 我在 Visual Studio Code 中创建了一个普通的 azure 函数应用程序。 当我在本地运行它时,它工作得很好。 当我将 pandas 添加到项目中时。 IE。将

import pandas as pd
添加到
__init__.py
,并将 pandas 添加到它破坏的
requirements.txt
文件中。 在运行时
func host start
它确实安装了需求,但在执行时失败。

> Executing task in folder basic-function-app: .venv\Scripts\python -m pip install -r requirements.txt <

Requirement already satisfied: azure-functions in c:\users\q\vscodeprojects\basic-function-app\.venv\lib\site-packages (from -r 
requirements.txt (line 5)) (1.9.0)
Collecting pandas
  Using cached pandas-1.4.1-cp39-cp39-win_amd64.whl (10.5 MB)
Collecting numpy>=1.18.5
  Downloading numpy-1.22.3-cp39-cp39-win_amd64.whl (14.7 MB)
     |████████████████████████████████| 14.7 MB 6.4 MB/s
Collecting pytz>=2020.1
  Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Collecting python-dateutil>=2.8.1
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, pytz, python-dateutil, numpy, pandas
Successfully installed numpy-1.22.3 pandas-1.4.1 python-dateutil-2.8.2 pytz-2021.3 six-1.16.0
WARNING: You are using pip version 21.2.4; however, version 22.0.4 is available.
You should consider upgrading via the 'C:\Users\Q\VSCodeProjects\basic-function-app\.venv\Scripts\python.exe -m pip install --upgrade pip' command.

Terminal will be reused by tasks, press any key to close it.

> Executing task in folder basic-function-app: .venv\Scripts\activate ; func host start <

Found Python version 3.9.10 (python3).

Azure Functions Core Tools
Core Tools Version:       4.0.3971 Commit hash: d0775d487c93ebd49e9c1166d5c3c01f3c76eaaf  (64-bit)
Function Runtime Version: 4.0.1.16815

[2022-03-11T18:51:51.953Z] File 'C:\Program Files\dotnet\dotnet.exe' is not found, 'dotnet' invocation will rely on the PATH environment variable.
[2022-03-11T18:51:52.752Z] File 'C:\Program Files\dotnet\dotnet.exe' is not found, 'dotnet' invocation will rely on the PATH environment variable.
[2022-03-11T18:51:53.167Z] File 'C:\Program Files\dotnet\dotnet.exe' is not found, 'dotnet' invocation will rely on the PATH environment variable.

Functions:

        test: [GET,POST] http://localhost:7071/api/test

For detailed output, run func with --verbose flag.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/2 POST http://127.0.0.1:55286/AzureFunctionsRpcMessages.FunctionRpc/EventStream application/grpc -    
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'gRPC - /AzureFunctionsRpcMessages.FunctionRpc/EventStream'
[2022-03-11T18:51:55.767Z] Worker process started and initialized.
[2022-03-11T18:51:55.791Z] Worker failed to load function: 'test' with function id: '26494c8b-064a-4e52-bc81-3e2fdc5bc196'.
[2022-03-11T18:51:55.792Z] Result: Failure
Exception: ModuleNotFoundError: No module named 'pandas'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 305, in _handle__function_load_request
    func = loader.load_function(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 42, in call
    raise extend_exception_message(e, message)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 40, in call
    return func(*args, **kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\loader.py", line 85, in load_function
    mod = importlib.import_module(fullmodname)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\Q\VSCodeProjects\basic-function-app\test\__init__.py", line 2, in <module>
    import pandas as pd
.

在虚拟环境中,可以看到pandas已经安装了,但是好像找不到。

当我明确地将 .venv 路径添加到脚本中时,它可以工作,但在我看来不需要这样做。

import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', '.venv/lib/site-packages')))

我错过了什么?

它明显激活了虚拟环境。 当我在相同的上下文中打开 python 时,它可以使用 pandas。

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\Q\VSCodeProjects\basic-function-app> & c:/Users/Q/VSCodeProjects/basic-function-app/.venv/Scripts/Activate.ps1
(.venv) PS C:\Users\Q\VSCodeProjects\basic-function-app> python
Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> pd.Timestamp('today')
Timestamp('2022-03-12 08:19:00.379798')
>>> 

还尝试直接执行

func host start
func start
形成 .venv

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\Q\VSCodeProjects\basic-function-app> & c:/Users/Q/VSCodeProjects/basic-function-app/.venv/Scripts/Activate.ps1
(.venv) PS C:\Users\Q\VSCodeProjects\basic-function-app> func start
Found Python version 3.9.10 (python3).

Azure Functions Core Tools
Core Tools Version:       4.0.3971 Commit hash: d0775d487c93ebd49e9c1166d5c3c01f3c76eaaf  (64-bit)
Function Runtime Version: 4.0.1.16815


Functions:

        test: [GET,POST] http://localhost:7071/api/test

For detailed output, run func with --verbose flag.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/2 POST http://127.0.0.1:56832/AzureFunctionsRpcMessages.FunctionRpc/EventStream application/grpc -    
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'gRPC - /AzureFunctionsRpcMessages.FunctionRpc/EventStream'
[2022-03-12T07:26:37.077Z] Worker process started and initialized.
[2022-03-12T07:26:37.092Z] Worker failed to load function: 'test' with function id: '1438e4d6-bd02-4fdc-b8b2-5f28a1294951'.       
[2022-03-12T07:26:37.096Z] Result: Failure
Exception: ModuleNotFoundError: No module named 'pandas'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 305, in _handle__function_load_request
    func = loader.load_function(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 42, in call
    raise extend_exception_message(e, message)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 40, in call
    return func(*args, **kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\loader.py", line 85, in load_function
    mod = importlib.import_module(fullmodname)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\Q\VSCodeProjects\basic-function-app\test\__init__.py", line 2, in <module>
    import pandas as pd
.
python azure-functions virtualenv
1个回答
0
投票

我也遇到过类似的情况, 我认为在本地,

  1. 配置你的解释器(或)
  2. 更新您的requirements.txt 文件

应该可以解决问题。

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