-
Notifications
You must be signed in to change notification settings - Fork 105
Debug Python Worker with Azure Functions Host
gavin-aguiar edited this page Jul 23, 2024
·
19 revisions
You can use our automation tool to streamline the setup process. Download and run the tool from the link below (available for Windows currently)
If you prefer to go through the setup steps manually, follow the instructions below.
- Clone this repo to your local machine
- Clone the Python Library repo to your local machine
- Clone the Azure Functions Host repo to your local machine
- 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 a Python version virtual environment .env under azure-functions-python-worker
py -<version> -m venv .env
- Activate the virtual environment in azure-functions-python-worker/.env
.env/Scripts/activate
- Run
pip install -e path/to/azure-functions-python-library
to install an editable package of Functions Python library - Run
pip install -e path/to/azure-functions-python-worker
to install an editable package of Functions Python worker - Install the debugpy library to enable remote debug
pip install debugpy
- Install the worker dependencies
pip install -e .[dev]
- 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.
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).
- In your Azure Functions Host folder, open the WebJobs.Script.sln with Visual Studio.
- Set WebJobs.Script.WebHost as the start up project.
- 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
- Use VSCode to open your azure-functions-python-worker
- Add a .vscode folder in the project root
- 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"
}
]
}
- Add a .vscode/settings.json file with the following content
{
"python.pythonPath": ".env\\Scripts\\python.exe"
}
- With WebJobs.Script solution opened in Visual Studio, open GrpcWorkerChannel.cs and put a breakpoint in method
SendWorkerInitRequest
, start WebJobs.Script.WebHost with F5.