- I encountered several issues getting VSCode + Azure Functions + Python to work properly.
- I was stuck in an error loop where the Azure Function Core Tools couldn’t find Python and ran into build errors like
"property is null"
. - After much trial and error, I found that upgrading the Core Tools wasn't the solution.
- Instead, a complete uninstall and reinstall worked at the target version of Azure Function Core Tools worked.
- Below are the consolidated steps I followed to set up a working Azure Function with local debugging.
Open an admin terminal (Command Prompt or PowerShell) and follow these commands to set up your environment.
-
Run the following commands to uninstall the Azure Function Core Tools:
npm uninstall -g azure-functions-core-tools choco uninstall azure-functions-core-tools --all-versions
-
Delete directories:
npm root -g
C:\Users\<YourUsername>\.azurefunctions
-
Open System Properties → Environment Variables. Remove any entries related to Azure Functions, such as
FUNCTIONS_WORKER_RUNTIME
. -
Verify that Azure Functions Core Tools is uninstalled:
func --version
This should return an error if uninstalled correctly.
-
Restart your system to clear cached values.
-
Reinstall the Azure Function Core Tools by running:
npm install -g azure-functions-core-tools@4 --unsafe-perm
-
Follow the Azure Quickstart Guide for creating Python functions.
-
Ensure you're using a compatible Python version (between
3.6
and3.11
).
- Open the VSCode Command Palette (
Ctrl+Shift+P
), search forPython: Create Environment
, and select a Python version between3.6
and3.11
. - The following steps will use the activated python virtual environment .venv.
-
Activate the virtual environment:
.venv\Scripts\activate
-
Verify the active Python version:
py --version
-
Create a new Azure Function project using the VSCode powershell terminal:
func init --python
-
Create a new function with an HTTP trigger:
- note, we'd use authentication in any envioronment beyond local testing.
func new --name helloworld --template "HTTP trigger" --authlevel "anonymous"
-
Using VSCode ensure Azurite is installed for local storage and update your
local.settings.json
for debugging.{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "python", "AzureWebJobsStorage": "UseDevelopmentStorage=true" } }
-
Create the following files in the
.vscode/
directory..vscode/launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Attach to Python Functions", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start" } ] }
.vscode/settings.json
{ "azureFunctions.deploySubpath": ".", "azureFunctions.scmDoBuildDuringDeployment": true, "azureFunctions.pythonVenv": ".venv", "azureFunctions.projectLanguage": "Python", "azureFunctions.projectRuntime": "~4", "debug.internalConsoleOptions": "neverOpen", "python.pythonPath": ".venv\\Scripts\\python.exe" }
.vscode/tasks.json
{ "version": "2.0.0", "tasks": [ { "type": "func", "command": "host start", "problemMatcher": "$func-watch", "isBackground": true, "dependsOn": "pipInstall" }, { "label": "pipInstall", "type": "shell", "windows": { "command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt" }, "problemMatcher": [] } ] }
-
Ensure your project runtime is set properly in the
.vscode/settings.json
file:"azureFunctions.projectRuntime": "~4"
-
Set breakpoints in your function and press
F5
to start debugging. -
If you encounter an error with:
@app.route(route="helloworld", auth_level=func.AuthLevel.Anonymous)
Change the auth_level to:
auth_level=func.AuthLevel.ANONYMOUS
-
Test the function by opening your browser and navigating to:
http://localhost:7071/api/helloworld?name=Test1
You should see the response:
Hello, Test1. This HTTP triggered function executed successfully.
Congratulations! You've successfully set up a local debug environment for building and testing Python Azure Functions. Happy coding! 🚀