如何持续交付部署在 Azure 中的 Python 函数应用程序?

问题描述

我第一次使用部署管道将 Python 函数应用部署到 Azure:

For the first time I deployed a Python function app to Azure using a deployment pipeline:

https://docs.microsoft.com/bs-latn-ba/azure/azure-functions/functions-how-to-azure-devops

使用 Kudu Zip deploy 将包部署到 Azure.

The package is deployed to Azure using Kudu Zip deploy.

我的 http 触发函数在本地(在 Windows 上)运行良好,但我在 Azure 上出现 500 个内部错误,因为它找不到模块 requests.

My http triggered function runs wonderfully locally (on Windows), but I have a 500 internal errors on Azure because it does not find the module requests.

异常:ModuleNotFoundError:没有名为请求"的模块

Exception: ModuleNotFoundError: No module named 'requests'

__init__.py 的导入:

imports of __init__.py:

import logging, requests, os
import azure.functions as func

如果我删除请求"依赖项,该函数可在 Azure 上运行(状态 200).

If I remove the 'requests' dependency the function works on Azure (status 200).

请求库由 requirement.txt 导入,并由构建管道复制到 .venv36/lib/site-packages/requests.

The requests library is imported by the requirement.txt and copied to the .venv36/lib/site-packages/requests by the build pipeline.

所以我想知道包中内置的虚拟环境 .venv36 是否被 Azure 中部署的功能使用.没有说明如何在 Azure 中激活虚拟环境.

So I am wondering if the virtual environment .venv36 that is built in the package is used by the function deployed in Azure. There is no indication about how to activate virtual environments in Azure.


解决方案

如果您将虚拟环境 worker_venv 命名为您链接的文档中的名称,它应该可以工作(假设您使用的是 Linux 环境用于您的管道).

If you name your virtual env worker_venv as named in the documentation you linked, it should work (assuming you are using a Linux environment for your pipeline).

但是,Python Azure Functions 文档很快就会更新,推荐的方法是不要从部署管道部署整个虚拟环境.相反,您希望将软件包安装在 .python_packages/lib/site-packages 中.

However, the Python Azure Functions documentation is to be updated very soon, and the recommended way would be to not deploy the entire virtual environment from your deployment pipeline. Instead, you'd want to install your packages in .python_packages/lib/site-packages.

你可以这样做——

pip3.6 install --target .python_packages/lib/site-packages -r requirements.txt

而不是--

python3.6 -m venv worker_venv
source worker_venv/bin/activate
pip3.6 install setuptools
pip3.6 install -r requirements.txt

它应该可以正常工作.

相关文章