Azure函数-异常:ImportError:导入numpy时出错:您不应该尝试从

qxgroojn  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(215)

我得到这个奇怪的消息,即使我已经安装通过皮普

Worker failed to function id 2c284181-1e21-4200-b5ea-b7d226696206.
[2023-11-30T03:24:02.694Z] Result: Failure
[2023-11-30T03:24:02.694Z] Exception: ImportError: Error importing numpy: you should not try to import numpy from
[2023-11-30T03:24:02.694Z]         its source directory; please exit the numpy source tree, and relaunch
[2023-11-30T03:24:02.694Z]         your python interpreter from there. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2023-11-30T03:24:02.695Z] Stack:   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/dispatcher.py", line 314, in _handle__function_load_request
[2023-11-30T03:24:02.695Z]     func = loader.load_function(
[2023-11-30T03:24:02.695Z]   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
[2023-11-30T03:24:02.695Z]     raise extend_exception_message(e, message)
[2023-11-30T03:24:02.695Z]   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
[2023-11-30T03:24:02.695Z]     return func(*args, **kwargs)
[2023-11-30T03:24:02.695Z]   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/loader.py", line 85, in load_function
[2023-11-30T03:24:02.695Z]     mod = importlib.import_module(fullmodname)
[2023-11-30T03:24:02.695Z]   File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
[2023-11-30T03:24:02.695Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2023-11-30T03:24:02.695Z]   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
[2023-11-30T03:24:02.695Z]   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
[2023-11-30T03:24:02.695Z]   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
[2023-11-30T03:24:02.696Z]   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
[2023-11-30T03:24:02.696Z]   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
[2023-11-30T03:24:02.696Z]   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
[2023-11-30T03:24:02.696Z]   File "/Users/zero/Documents/Github/project/fc/__init__.py", line 7, in <module>
[2023-11-30T03:24:02.696Z]     import numpy as np
[2023-11-30T03:24:02.696Z]   File "/Users/zero/Library/Python/3.9/lib/python/site-packages/numpy/__init__.py", line 135, in <module>
[2023-11-30T03:24:02.696Z]     raise ImportError(msg) from e

字符串
初始化.py

import os
import re
import logging
from pathlib import Path
import numpy as np

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('A function processed a request.')

    return func.HttpResponse(
        "success",
        status_code=200,
    )


requirements.txt

numpy


的数据

n53p2ov0

n53p2ov01#

该错误可能是由于NumPy包安装不正确造成的。
要解决此问题,请执行以下操作:

  • 转到 *Project文件夹 * 并启动 *Python解释器 *(命令面板=>搜索Python: Select Interpreter)。
  • 在终端中运行以下命令,重新安装NumPy
pip uninstall numpy
pip install numpy

字符串
我使用***Python版本3.11.6***创建了一个简单的Python Azure函数,并导入了***NumPy***Package。

验证码:

import azure.functions as func
import logging

import numpy as np

app = func.FunctionApp()

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

    arr = np.array([1, 2, 3])

    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 with {arr}")
    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
        )

requirements.txt:

azure-functions
numpy

  • 创建并激活虚拟环境:
py -m venv .venv  
.venv\scripts\activate

  • 使用命令pip install -r requirements.txt安装所需的软件包:

x1c 0d1x的数据

    • 安装.venv/Lib/site-packages下的包:*


输出:



参考资料:

Troubleshooting — NumPy v1.26 Manual

相关问题