Reputation: 1041
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?
Upvotes: 5
Views: 6960
Reputation: 1041
For anyone facing similar concerns, I have ended up with following setting thanks to answer provided by 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"
}
}
]
}
Upvotes: 2
Reputation: 10321
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": []
}
]
}
Upvotes: 6