【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]

相关文章
|
2月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
294 11
|
4月前
|
Linux
Linux环境下的UDEV机制及其与守护进程的关联
实际使用时管理员需要熟悉编写合适udev rules去满足特殊需求;同时也需要注意避免编写过度复杂导致无法预料结果rules.UDEVD虽然稳健但错误配置可能导致无法预料问题因此需谨慎处理相关配置工作.
183 16
|
2月前
|
缓存 移动开发 JavaScript
如何优化UniApp开发的App的启动速度?
如何优化UniApp开发的App的启动速度?
558 139
|
2月前
|
移动开发 JavaScript weex
UniApp开发的App在启动速度方面有哪些优势和劣势?
UniApp开发的App在启动速度方面有哪些优势和劣势?
351 137
|
2月前
|
数据采集 JavaScript 前端开发
开发比分App?你缺的不是程序员
开发体育比分App,关键不在代码,而在懂体育、懂数据、懂用户。明确定位、理清需求、选好数据源,再找专业的产品、数据与技术人才协同,才能少走弯路。程序员最后入场,效率最高。
232 154
|
3月前
|
移动开发 小程序 Android开发
基于 uni-app 开发的废品回收类多端应用功能与界面说明
本文将对一款基于 uni-app 开发的废品回收类多端应用,从多端支持范围、核心功能模块及部分界面展示进行客观说明,相关资源信息也将一并呈现。
166 0
|
2月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
315 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
2月前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
284 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡

热门文章

最新文章