【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-module notfound
完整的错误消息为:
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-module notfound ) 进行逐步排查,还是非常有帮助的。
但是当前问题,并不是没有尝试安装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>).