【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 Server : https://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]
【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误的更多相关文章
- Linux环境下JDK/Eclipse一键安装脚本
-------------------------------------------------------------------- author:jiangxin Email:jiangxinn ...
- linux环境下,接着lnmp,安装redis
linux环境下,安装redis 操作记录: 回到家目录 cd ~查看 ls进入 lump cd lnmp1.3-fullls??? sudo ./addons.sh //---进入后选 ...
- 在Linux环境下采用压缩包方式安装JDK 13
本文地址:https://www.cnblogs.com/oberon-zjt0806/p/11663731.html 可以,转载,出处,格式,懂?? 什么是JDK?? 好吧如果你不知道这个问题的话我 ...
- [Python学习] Linux环境下的Python配置,必备库的安装配置
1.默认Python安装情况 一般情况,Linux会预装Python的,版本较低,比如Ubuntu15的系统一般预装的是Python2.7.10. 使用命令:which python可以查看当前的py ...
- Linux环境下SVN服务器端的安装与配置
最近尝试了下在Linux(CentOS6.5)环境下安装与配置SVN服务器端,安装过程中碰到了一些问题,参看了网友们分享的一些心得,并通过自己实际的操作,最终安装与配置成功!总的来说网上的说法芸芸,大 ...
- linux环境下的python安装过程
一.下载python源码包 打开ubuntu下的shell终端,通过wget命令下载python源码包,如下图所示: wget https://www.python.org/ftp/python/3. ...
- linux环境下的python安装过程(含setuptools)
这里我不想采用诸如ubuntu下的apt-get install方式进行python的安装,而是在linux下采用源码包的方式进行python的安装. 一.下载python源码包 打开ubuntu下的 ...
- Linux环境下的Docker的安装和部署、学习-一
CentOS Docker 安装Docker支持以下的CentOS版本:CentOS 7 (64-bit)CentOS 6.5 (64-bit) 或更高的版本 前提条件目前,CentOS 仅发行版本中 ...
- Linux环境下搭建python+selenium+webdriver环境
1.下载并安装python,一般安装linux系统,自带有python,则python不用安装.要下载可以在官网上下载: 或者使用下面命令安装: sudo apt-get install python ...
- Linux环境下Node.js的安装配置
1. 官网下载Node.js 2. 安装Node.js 根据下载内容的不同,提供三种安装方法,选择自己喜欢的方式 2.1. 绿色免安装版(Linux(.tar.gz)) 解压Node-XX ...
随机推荐
- postman数据驱动(.csv文件)
做api测试的时候同一个接口我们会用大量的数据(正常流/异常流)去验证,要是一种场 景写一个接口的话相对于比较麻烦,这个时候就可以使用数据驱动来实现 1.本地创建一个txt文件,第一行写上字段名,多个 ...
- 30岁程序媛求职路复盘:文转码+失业半年+PHP如何涨薪5K!?
这篇文章来自一位群友的分享: 这篇文章写于下班路上,刚刚入职不久,我想再冲刺一下大厂,阳哥建议我坚持总结打卡,可以尝试写写博客. 那我就从这篇开始吧,希望开个好头! 上班的感觉真好 今天是入职的第二周 ...
- 文盘Rust -- 如何把配置文件打包到二进制文件里
在实际开发中,经常会遇到各种不同的配置文件.通常,程序运行的各种配置从外部读取,以增强应用配置的灵活性.java 生态中的 springboot 提供了这种设计的典范.springboot 的应用程 ...
- js中forEach的用法、forEach如何跳出循环、forEach与for之间的区别
定义和用法 forEach() 调用数组的每个元素,并将元素传递给回调函数. 注意: forEach() 对于空数组是不会执行回调函数的. 用法: array.forEach(function(cur ...
- 数据仓库(4)基于维度建模的数仓KimBall架构
基于维度建模的KimBall架构,将数据仓库划分为4个不同的部分.分别是操作型源系统.ETL系统.数据展现和商业智能应用,如下图. 操作型源系统,指的就是面向用户的各类系统,如app.网站.E ...
- kettle(docker版)系列文章02---hello world
目标:将mysql库中表的数据定时转到另外一个表中 1.在mysql中新建目标表test_kettle,有字段id,productName,createtime 2.连接mysql数据库 DB连接-- ...
- Go 泛型之明确使用时机与泛型实现原理
目录 一.引入 二.何时适合使用泛型? 场景一:编写通用数据结构时 场景二:函数操作的是 Go 原生的容器类型时 场景三:不同类型实现一些方法的逻辑相同时 三.Go 泛型实现原理 Stenciling ...
- 在K8S中,静态、动态、自主式Pod有何区别?
在Kubernetes(简称K8s)中,静态Pod.自主式Pod和动态Pod是不同管理方式下的Pod类型,它们的区别主要体现在创建和管理方式上: 静态Pod: 静态Pod是由kubelet直接管理的, ...
- go中的类型转换成interface之后如何复原
go中interface转换成原来的类型 首先了解下interface 什么是interface? 如何判断interface变量存储的是哪种类型 fmt 反射 断言 来看下interface的底层源 ...
- Spring一套全通5—SSM整合
百知教育 - Spring系列课程 - MVC框架整合 第一章.MVC框架整合思想 1. 搭建Web运行环境 <dependency> <groupId>javax.servl ...