当我尝试在本地运行我的 azure 函数时,VS 代码中的 python 设置遇到了一些不一致的问题。我试图避免使用 VS code 自动为 azure 函数项目设置的“venv”环境,而是使用我制作的预先创建的 conda 环境,并安装了所有要求。只是为了澄清一下,这是关于本地部署和不是 azure 门户。
myfunc__init__.py:
import json
import logging
import time
import azure.functions as func
import pandas as pd # Import Error happens here!
def main(req: func.HttpRequest) -> func.HttpResponse:
...
.vscode\Settings.json:
{
// Local Machine Conda VENV (Define CONDAPATH in Windows Environment)
"python.condaPath": "%CONDAPATH%",
"python.pythonPath": "%CONDAPATH%\\envs\\azure\\python.exe",
"azureFunctions.pythonVenv": "%CONDAPATH%\\envs\\azure",
// Created Local VENV by VS Code (pythonPath is difference for MAC vs Windows)
//"azureFunctions.pythonVenv": ".venv",
// Azure Function Stuff
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~2",
"azureFunctions.preDeployTask": "func: pack --build-native-deps",
"debug.internalConsoleOptions": "neverOpen",
}
注意:如果我将 %CONDAPATH%
替换为 conda 的实际绝对路径,问题仍然存在。
为了以防万一,launch.json
配置如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "Linux_PyFunc",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
当 VS Code 运行这些函数时,部署会顺利完成,并生成本地链接。一旦我通过 Postman 调用该函数,返回的是 HTTP 500 状态,这是由于无法导入 pandas 且错误模块未找到。
如果我在 settings.json
中设置 "azureFunctions.pythonVenv": ".venv"
,函数将在本地部署,一旦触发/调用,它就会返回 HTTP 200
状态和正确响应。
所以,这让我想到一个问题,VS code 是否支持用于 azure 函数部署的 conda 环境,如果是,我在这里缺少什么?
最佳答案
这是我为获得 conda 环境而不是 venv 所做的事情
查看 settings.json 文件。由于我已经安装了 python 扩展,并且已经为该项目配置了解释器,因此我有一个名为 python.pythonPath
的设置。我想使用这个 python 而不是 venv,所以我注释掉了 venv 设置。
{
"azureFunctions.deploySubpath": "./functions/",
"azureFunctions.scmDoBuildDuringDeployment": true,
// "azureFunctions.pythonVenv": "../.venv", // Ignore not going to use
"azureFunctions.projectLanguage": "Python",
...
"python.pythonPath": "C:\\path\\to\\Anaconda3\\envs\\myenviron\\python.exe",
...
}
接下来编辑tasks.json。请注意,有一个 pipInstall 任务。我更改了 widows 命令以使用设置中定义的 python.pythonPath
。
旧值类似于 "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install...
新值是 "command":“${config:python.pythonPath} -m pip install...
{
"version": "2.0.0",
"tasks": [
...
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:python.pythonPath} -m pip install -r ${config:azureFunctions.deploySubpath}/requirements.txt"
},
"windows": {
"command": "${config:python.pythonPath} -m pip install -r ${config:azureFunctions.deploySubpath}\\requirements.txt"
},
"linux": {
"command": "${config:python.pythonPath} -m pip install -r ${config:azureFunctions.deploySubpath}/requirements.txt"
},
"problemMatcher": []
}
]
}
关于python - 使用 conda 环境在 VS code 中部署 Azure Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59363653/