python - 在 VS Code 和 virtualenv 中使用 Azure Functions 时找不到 Numpy 模块

标签 python numpy azure-functions virtualenv

我刚开始使用 azure 函数,并尝试在本地使用 VS Code 和 Azure Functions 扩展编写一个小示例。

例子:

# First party libraries
import logging

# Third party libraries
import numpy as np
from azure.functions import HttpResponse, HttpRequest


def main(req: HttpRequest) -> HttpResponse:
    seed = req.params.get('seed')

    if not seed:
        try:
            body = req.get_json()
        except ValueError:
            pass
        else:
            seed = body.get('seed')

    if seed:
        np.random.seed(seed=int(seed))
        r_int = np.random.randint(0, 100)
        logging.info(r_int)
        return HttpResponse(
            "Random Number: " f"{str(r_int)}", status_code=200
            )
    else:
        return HttpResponse(
            "Insert seed to generate a number",
            status_code=200
            )

当全局安装 numpy 时,这段代码工作正常。但是,如果我只在虚拟环境中安装它,则会出现以下错误:

*Worker failed to function id 1739ddcd-d6ad-421d-9470-327681ca1e69.
[15-Jul-20 1:31:39 PM] Result: Failure
Exception: ModuleNotFoundError: No module named 'numpy'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound*

多次查看虚拟环境中安装了numpy,并且在.vscode/settings.json文件中也指定了环境。

virtualenv“worker_venv”的 pip 卡住:

$ pip freeze
azure-functions==1.3.0
flake8==3.8.3
importlib-metadata==1.7.0
mccabe==0.6.1
numpy==1.19.0
pycodestyle==2.6.0
pyflakes==2.2.0
zipp==3.1.0

.vscode/settings.json 文件:

{
  "azureFunctions.deploySubpath": ".",
  "azureFunctions.scmDoBuildDuringDeployment": true,
  "azureFunctions.pythonVenv": "worker_venv",
  "azureFunctions.projectLanguage": "Python",
  "azureFunctions.projectRuntime": "~2",
  "debug.internalConsoleOptions": "neverOpen"
}

我试图在文档中找到一些东西,但没有找到任何关于虚拟环境的具体内容。我不知道我是否遗漏了什么?

编辑:顺便说一句,我在 Windows 10 机器上

编辑:我在下图中包含了我项目的文件夹结构

enter image description here

编辑:在下图中添加了虚拟环境 Lib 文件夹的内容

enter image description here

编辑:使用下面的 pip install numpy 命令添加了终端的屏幕截图

enter image description here

编辑:使用新的虚拟环境创建了一个新项目并重新安装了 numpy,如下截图,问题仍然存在。

enter image description here

编辑:在下面添加了 launch.json 代码

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to Python Functions",
      "type": "python",
      "request": "attach",
      "port": 9091,
      "preLaunchTask": "func: host start"
    }
  ]
}

已解决

所以问题既不在于 python,也不在于 VS Code。问题是我的机器(新笔记本电脑)上的执行策略设置为受限,因此无法运行 .venv\Scripts\Activate.ps1 脚本。

要解决此问题,只需使用管理员权限打开 powershell 并运行 set-executionpolicy remotesigned。重启 VS Code 一切正常

我没有看到错误,因为在终端中发生了很多登录 当你开始天蓝色。我会将@HuryShen 的答案标记为正确,因为这些评论让我找到了解决方案。谢谢大家

最佳答案

这个问题,我不清楚你是在本地运行还是在azure cloud上运行报错。所以针对这两种情况给出两种建议。

1. 如果您在azure上运行该函数时出现错误,您可能没有成功安装模块。将函数从本地部署到 azure 时,需要将模块添加到 requirements.txt(如 Anatoli 在评论中提到的)。您可以通过以下命令自动生成 requirements.txt:

pip freeze > requirements.txt

之后,我们可以在requirements.txt中找到numpy==1.19.0enter image description here

现在,通过以下命令将函数从本地部署到 azure,它将在 azure 上成功安装模块并在 azure 上正常工作。

func azure functionapp publish <your function app name> --build remote

2. 如果在本地运行函数时出现错误。由于您提供了安装在 worker_venv 中的模块,看来您已经成功安装了 numpy 模块。我也在本地测试它,安装 numpy 并且它工作正常。所以我认为您可以检查您的虚拟环境 (worker_venv) 是否存在于正确的位置。下面是我在本地VS代码中的函数结构,请检查你的虚拟环境是否和我的在同一个位置。

enter image description here

-----更新------

运行命令设置执行策略,然后激活虚拟环境:

set-executionpolicy remotesigned
.venv\Scripts\Activate.ps1

关于python - 在 VS Code 和 virtualenv 中使用 Azure Functions 时找不到 Numpy 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62916388/

相关文章:

python - 在加载 Keras 保存模型时,是否有人得到 "AttributeError: ' str' 对象没有属性 'decode'"

arrays - 不使用 for 循环的数组广播

python - 在 Python 中读取和切片二进制数据文件的最快方法

涉及单元邻居的两个相同形状的 numpy 数组之间的 Python 交互

c# - 从 Azure Function 读取设置

c# - Azure Durable Functions - OrchestrationTrigger 连续执行第一个等待的事件

python - 如何在 python3 中注释生成器?

python - hash() 如何计算元组的哈希值?

python - 将数据传递到 Django 表单中

azure - 将第三方身份验证提供程序与 Azure Functions 结合使用