python 使用conda环境在VS代码中部署Azure Functions

ej83mcc0  于 4个月前  发布在  Python
关注(0)|答案(2)|浏览(66)

我在尝试本地运行我的Azure函数时,在VS代码中的Python设置有一点不一致的问题。我试图避免使用VS代码自动为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",
}

**注意:**如果我用conda的实际绝对路径替换%CONDAPATH%,问题仍然存在。

在需要时,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与错误模块未找到。
如果我在settings.json中设置"azureFunctions.pythonVenv": ".venv",函数将在本地部署,一旦被触发/调用,它将返回HTTP 200状态和正确的响应。
所以,这给我带来了一个问题,如果VS代码支持Azure函数部署的conda环境,如果是这样,我在这里错过了什么?

a2mppw5e

a2mppw5e1#

这是我所做的,以获得康达环境,而不是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": []
        }
    ]
}

sqougxex

sqougxex2#

对于任何人面临类似的问题,我已经结束了以下设置感谢布里格提供的答案:

.vscode\settings.json

{
    // 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",    
}

字符串

.vscode\tasks.json

{
    "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"
            }
        }
    ]
}

相关问题