Answer a question

I have been having a little bit of inconsistency issue with python settings in VS code while trying to run my azure functions locally. I am trying to avoid using the "venv" environment that VS code automatically sets for an azure function project and instead use a pre-created conda environment I made and have all the requirement installed in.Just to clarify, this is about local deployment and not azure portal.

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

Note: If I replace the %CONDAPATH% with an actual absolute path to conda, the issue remains.

Just in case of need, launch.json is configured as bellow:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Linux_PyFunc",
      "type": "python",
      "request": "attach",
      "port": 9091,
      "preLaunchTask": "func: host start"
    }
  ]
}

When VS Code runs the functions, the deployment completes without issues and the local links are generated. Once I call the function via Postman, the return is HTTP 500 status which is due to not being able to import pandas with error module not found.

If I set "azureFunctions.pythonVenv": ".venv" in settings.json the functions gets deployed locally and once triggered/called, it returns HTTP 200 status and proper response.

So, this brings me to the question, if VS code supports conda environment for azure function deployment and if so, what am I missing here?

Answers

Here is what I did to get conda environment instead of venv

Look in the settings.json file. Since I already had the python extension installed and I had already configured my interpreter for this project, I had a setting called python.pythonPath. I want to use this python instead of the venv so I comment out the venv setting.

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

Next edit the tasks.json. Notice there is a pipInstall task. I changed the widows command to use my python.pythonPath defined in settings.

The old value was something like "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install... the new value is "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": []
        }
    ]
}
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐