Skip to content

Debug Python Worker with Azure Functions Host

gavin-aguiar edited this page Jul 23, 2024 · 19 revisions

Try the Automated Setup (Optional)

You can use our automation tool to streamline the setup process. Download and run the tool from the link below (available for Windows currently)

Download Automation Tool

If you prefer to go through the setup steps manually, follow the instructions below.

Prerequisites

  1. Clone this repo to your local machine
  2. Clone the Python Library repo to your local machine
  3. Clone the Azure Functions Host repo to your local machine
  4. Make sure your machine has installed Python Version, supported versions info here, Visual Studio 2022 and .NET 6.0 (can be installed from/with Visual Studio 2022 installer)

Create Python Virtual Environment

  1. Create a Python version virtual environment .env under azure-functions-python-worker py -<version> -m venv .env
  2. Activate the virtual environment in azure-functions-python-worker/.env .env/Scripts/activate
  3. Run pip install -e path/to/azure-functions-python-library to install an editable package of Functions Python library
  4. Run pip install -e path/to/azure-functions-python-worker to install an editable package of Functions Python worker
  5. Install the debugpy library to enable remote debug pip install debugpy
  6. Install the worker dependencies pip install -e .[dev]
  7. Compile the .proto Protobuf definition into _pb2.py file cd tests && invoke -c test_setup build-protos. Note: If you have any .proto changes, make sure you update the .proto file and then run the setup.py build command.

Link Python Worker to Python Virtual Environment

In azure-functions-python-worker/python/test/worker.config.json, change defaultExecutablePath to your virtual environment interpreter (e.g. C:/Users/uname/azure-functions-python-worker/.env/Scripts/python.exe).

Setup Azure Functions Host for debugging

  1. In your Azure Functions Host folder, open the WebJobs.Script.sln with Visual Studio.
  2. Set WebJobs.Script.WebHost as the start up project.
  3. In WebJobs.Script.WebHost -> Properties -> launchSettings.json, change the environment variables into following
{
  "profiles": {
    "WebJobs.Script.WebHost": {
      "commandName": "Project",
      "environmentVariables": {
        "languageWorkers:python:workerDirectory": "C:/path/to/your/azure-functions-python-worker/python/test",
        "languageWorkers:python:arguments": "-m debugpy --listen 127.0.0.1:9091",
        "AzureWebJobsScriptRoot": "C:/path/to/your/functionapp/folder",
        "AzureWebJobsStorage": "a storage account connection string",
        "FUNCTIONS_WORKER_RUNTIME": "python"
        "AZURE_FUNCTIONS_ENVIRONMENT": "Development",
        "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
        "AzureFunctionsJobHost__extensionBundle__downloadPath": "C:\\Users\\<user>\\.azure-functions-core-tools\\Functions\\ExtensionBundles\\Microsoft.Azure.Functions.ExtensionBundle"
      }
    }
  }
}

You're now able to debug the function host using your local Python worker

Setup Azure Functions Python Worker for debugging

  1. Use VSCode to open your azure-functions-python-worker
  2. Add a .vscode folder in the project root
  3. Add a .vscode/launch.json file with the following content
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach (Remote Debug)",
            "type": "python",
            "request": "attach",
            "port": 9091,
            "host": "localhost"
        }
    ]
}
  1. Add a .vscode/settings.json file with the following content
{
    "python.pythonPath": ".env\\Scripts\\python.exe"
}
  1. With WebJobs.Script solution opened in Visual Studio, open GrpcWorkerChannel.cs and put a breakpoint in method SendWorkerInitRequest, start WebJobs.Script.WebHost with F5.