Skip to content

Instantly share code, notes, and snippets.

@damien-johnston
Last active November 15, 2024 14:26
Show Gist options
  • Save damien-johnston/534c7e23c093d6b1664cca0f78add287 to your computer and use it in GitHub Desktop.
Save damien-johnston/534c7e23c093d6b1664cca0f78add287 to your computer and use it in GitHub Desktop.
VSCode + Azure Functions + Python for Local Development and Debugging

Troubleshooting VSCode, Azure Functions, and Python Issues

  • 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.

Step-by-Step Instructions

Open an admin terminal (Command Prompt or PowerShell) and follow these commands to set up your environment.

Uninstall Azure Function Core Tools and Clean Up

  1. 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
    
  2. Delete directories:

    npm root -g
    
    C:\Users\<YourUsername>\.azurefunctions
    
  3. Open System PropertiesEnvironment Variables. Remove any entries related to Azure Functions, such as FUNCTIONS_WORKER_RUNTIME.

  4. Verify that Azure Functions Core Tools is uninstalled:

    func --version

    This should return an error if uninstalled correctly.

  5. Restart your system to clear cached values.

  6. Reinstall the Azure Function Core Tools by running:

    npm install -g azure-functions-core-tools@4 --unsafe-perm

Set Up a Python Environment in VSCode

  1. Follow the Azure Quickstart Guide for creating Python functions.

  2. Ensure you're using a compatible Python version (between 3.6 and 3.11).

  • Open the VSCode Command Palette (Ctrl+Shift+P), search for Python: Create Environment, and select a Python version between 3.6 and 3.11.
  • The following steps will use the activated python virtual environment .venv.
  1. Activate the virtual environment:

    .venv\Scripts\activate
  2. Verify the active Python version:

    py --version

Create an Azure Function Project

  1. Create a new Azure Function project using the VSCode powershell terminal:

    func init --python
  2. 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"
  3. 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"
                }
    }

Configure Debugging in VSCode

  1. 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"

Running and Debugging Locally

  1. Set breakpoints in your function and press F5 to start debugging.

  2. 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
  3. 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.
    

Conclusion

Congratulations! You've successfully set up a local debug environment for building and testing Python Azure Functions. Happy coding! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment