【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误

简介: 【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误

问题描述

在Azure中创建Function App(函数应用), 用以运行Python代码(Python Version 3.7)。 通过VS Code创建一个HttpTrigger的Function,其中使用到了 psycopg2 模块,以便连接  Azure Database for PostgreSQL 数据库

 

当通过VS Code发布到Azure后,请求 Function URL 出错。通过高级工具(Kudu:https://<xxxxxxxx>.scm.chinacloudsites.cn/)登录到Logfiles中查看到错误消息为:

错误消息显示:ModuleNotFoundError: No module named 'psycopg2'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound

完整的错误消息为:

2021-11-24T01:55:27.767 [Information] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=fb46530a-60db-4d44-b705-e81f805c9743)
2021-11-24T01:55:28.640 [Information] Worker process started and initialized.
2021-11-24T01:55:28.762 [Error] Executed 'Functions.HttpTrigger1' (Failed, Id=fb46530a-60db-4d44-b705-e81f805c9743, Duration=1095ms)
Result: Failure
Exception: ModuleNotFoundError: No module named 'psycopg2'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
Stack:   File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 305, in _handle__function_load_request
    func = loader.load_function(
  File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
    raise extend_exception_message(e, message)
  File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
    return func(*args, **kwargs)
  File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 85, in load_function
    mod = importlib.import_module(fullmodname)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/site/wwwroot/HttpTrigger1/__init__.py", line 2, in <module>
    import psycopg2

 

问题分析

当遇见模块无法安装的时候,可以参考 TroubleShooting Guide (https://aka.ms/functions-modulenotfound)  进行逐步排查,还是非常有帮助的。

但是当前问题,并不是没有尝试安装psycopg2这个模块,而是虽然在Function的 requirements.txt 文件中已经添加了psycopg2,但是在安装的过程中出错。为了更明确的知道错误的消息,直接SSH连接到Function App所运行的Linux实例。通过 python -m pip install psycopg2 查看错误信息:

 

错误文本:

root@1ac8c89f91a9:~#  python -m pip install psycopg2
Collecting psycopg2
  Downloading psycopg2-2.9.2.tar.gz (380 kB)
     |████████████████████████████████| 380 kB 274 kB/s
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-zvgqohj2/psycopg2_46223be3606547c5bbdd49a650506
7d8/setup.py'"'"'; __file__='"'"'/tmp/pip-install-zvgqohj2/psycopg2_46223be3606547c5bbdd49a6505067d8/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file
__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exe
c(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-4y8snj3a
         cwd: /tmp/pip-install-zvgqohj2/psycopg2_46223be3606547c5bbdd49a6505067d8/
    Complete output (23 lines):
    running egg_info
    creating /tmp/pip-pip-egg-info-4y8snj3a/psycopg2.egg-info
    writing /tmp/pip-pip-egg-info-4y8snj3a/psycopg2.egg-info/PKG-INFO
    writing dependency_links to /tmp/pip-pip-egg-info-4y8snj3a/psycopg2.egg-info/dependency_links.txt
    writing top-level names to /tmp/pip-pip-egg-info-4y8snj3a/psycopg2.egg-info/top_level.txt
    writing manifest file '/tmp/pip-pip-egg-info-4y8snj3a/psycopg2.egg-info/SOURCES.txt'
    Error: pg_config executable not found.
    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    or with the pg_config option in 'setup.cfg'.
    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.
    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).
    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/33/ed/79434011d773e5ea4c51262f6ebfb86680c2908d7677f31ebccd5aa9f1b3/psycopg2-2.9.2.tar.gz#sha256=a84da9fa8
91848e0270e8e04dcca073bc9046441eeb47069f5c0e36783debbea (from https://pypi.org/simple/psycopg2/) (requires-python:>=3.6). Command errored out with exit status 1: pyt
hon setup.py egg_info Check the logs for full command output.

 

真正的错误就是 pg_config 是必须的,但是Linux实例上并没有配置。而解决办法就是使用 psycopg2-binary 包代替,接着使用  python -m pip install psycopg2-binary 来测试是否可以安装成功。

 

验证成功!

所以为了能在 Python Function 中使用 psycopg2 模块,需要在 requirements.txt 文件中,使用 psycopg2-binary 代替 psycopg2。 其他的(如代码中的 import psycopg2)则无需修改。

 

参考资料

在 Azure 中使用 Visual Studio Code 创建 Python 函数https://docs.azure.cn/zh-cn/azure-functions/create-first-function-vs-code-python

 

Use Python to connect and query data in Azure Database for PostgreSQL - Single Serverhttps://docs.microsoft.com/en-us/azure/postgresql/connect-python#prerequisites

Install psycopg2 using pip install psycopg2-binary in a terminal or command prompt window.

 

Using psycopg2 in Azure Functions : https://iotespresso.com/using-psycopg2-in-azure-functions/

psycopg2 is not a standalone package. It is built using libpq-dev package, during installation. Now, psycopg2 depends on the host OS to find this library. And Azure Functions don’t have libpq, as the error above clearly indicates.

You may be wondering that we can perhaps simply add libpq-dev to requirements.txt. But that doesn’t work. You will get the error:

No matching distribution found for libpq-dev

Thus, the required solution is to use a package of psycopg2 that doesn’t require libpq. psycopg2-binary is exactly that. I hope this provided a satisfactory explanation.

 

 

[End]

相关文章
|
3月前
|
存储 安全 数据安全/隐私保护
【Azure Function App】在Function App中使用System Managed Identity访问Storage Account
本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。
131 20
|
5月前
|
JavaScript 前端开发 Android开发
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
158 13
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
2月前
|
数据采集 Web App开发 JavaScript
基于Selenium的Python爬虫抓取动态App图片
基于Selenium的Python爬虫抓取动态App图片
254 68
|
2月前
|
安全 Linux 开发工具
【Azure Function】分享把Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤
本文介绍了将Azure Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤。.NET 6.0作为长期支持版本,生命周期至2024年11月结束。为确保持续支持,建议升级至更新版本。升级步骤包括安装.NET 8 SDK、更新Azure Functions Core Tools、修改项目文件目标框架为net8.0、更新兼容的NuGet包、本地测试以及重新发布到Azure。更多详细信息可参考官方文档。
110 9
|
3月前
|
数据采集 数据可视化 数据挖掘
基于Python的App流量大数据分析与可视化方案
基于Python的App流量大数据分析与可视化方案
|
6月前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
162 64
|
5月前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
138 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
5月前
|
数据采集 JavaScript Android开发
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
155 7
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
5月前
|
JavaScript API
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
135 7
|
6月前
|
Dart 前端开发 架构师
【01】vs-code如何配置flutter环境-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈-供大大的学习提升
【01】vs-code如何配置flutter环境-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈-供大大的学习提升
349 26

热门文章

最新文章

推荐镜像

更多