在尝试在本地运行我的azure函数时,VS代码中的python设置一直存在一些不一致的问题。我试图避免使用VS代码自动为azure函数项目设置的“ venv”环境,而是使用我创建的预先创建的conda环境并安装所有要求。请澄清一下,这是关于本地部署和不是天青的门户网站。
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
,这是由于无法import pandas
并没有找到错误模块。
如果我在"azureFunctions.pythonVenv": ".venv"
中设置了settings.json
,则这些函数会在本地部署,并且一旦被触发/调用,它将返回HTTP 200
状态和正确的响应。
因此,这使我想到了一个问题,即VS代码是否支持conda环境用于azure函数部署,如果是,我在这里缺少什么?
答案 0 :(得分:1)
对于任何面临类似问题的人,由于 Brig 提供的答案,我最终得到了以下设置:
{
// python.pythonPath is machine-dependent, AVOIDING a set value here
// Local Machine Conda VENV (Define CONDAPATH in Windows Environment)
"python.condaPath": "${env:CONDAPATH}/Library/bin/conda.bat",
"python.pythonPath": "${env:CONDAPATH}/envs/azure/python.exe",
// Needs `pyvenv.cfg` present in venv path?
// "azureFunctions.pythonVenv": "${env:CONDAPATH}/envs/azure",
// Created Local VENV by VS Code (pythonPath is difference for MAC vs Windows)
"azureFunctions.pythonVenv": ".venv",
// Windows pythonPath
// "python.pythonPath": "src/.venv/Scripts/python.exe",
// MAC pythonPath
// "python.pythonPath": "src/.venv/bin/python",
// Azure Function Stuff
"azureFunctions.deploySubpath": "src",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~3",
"azureFunctions.preDeployTask": "func: pack --build-native-deps",
"debug.internalConsoleOptions": "neverOpen",
}
{
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-watch",
"isBackground": true,
"dependsOn": "pipInstall",
"options": {
"cwd": "${workspaceFolder}/src"
}
},
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": [],
"options": {
"cwd": "${workspaceFolder}/src"
}
}
]
}
答案 1 :(得分:0)
这是我要获得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",
...
}
接下来,编辑task.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": []
}
]
}