Skip to content

Debug Python Worker with Azure Functions Host

Hanzhang Zeng edited this page Jan 26, 2021 · 20 revisions

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 3.6/3.7/3.8 and .NETCore SDK 2.2/3.0

Create Python Virtual Environment

  1. Create a Python 3.6/3.7/3.8 virtual environment .env under azure-functions-python-worker py -3.8 -m venv .env
  2. Activate the virtual environment in azure-functions-python-worker/.env
  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 ptvsd library to enable remote debug pip install ptvsd~=4.3.2

Link Python Worker to Python Virtual Environment

  1. Run pip install debugpy in your virtual envrionment.
  2. 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",
        "AZURE_FUNCTIONS_ENVIRONMENT": "Development"
      }
    }
  }
}

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. Breakpoint SendWorkerInitRequest, start WebJobs.Script.WebHost with F5.