How to Use Azure Function with Python: Simple Guide
To use
Azure Function with Python, create a Python function app in Azure, write your Python code in the function, and deploy it using Azure CLI or VS Code. Azure Functions run your Python code in response to events like HTTP requests or timers.Syntax
An Azure Function in Python typically has a function.json file to define triggers and bindings, and a __init__.py file with the Python code. The main Python function takes req (request) and context parameters for input and environment info.
- function.json: Defines how the function is triggered (e.g., HTTP trigger).
- __init__.py: Contains the Python code to run.
- req: The input request object.
- context: Provides runtime info and logging.
json + python
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "post"]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
# __init__.py
import logging
import azure.functions as func
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)Example
This example shows a simple HTTP-triggered Azure Function in Python that greets the user by name. It reads the name parameter from the URL or request body and returns a greeting.
python
# __init__.py import logging import azure.functions as func def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') if name: return func.HttpResponse(f"Hello, {name}!") else: return func.HttpResponse( "Please pass a name on the query string or in the request body", status_code=400 )
Output
HTTP 200 OK
Hello, Alice!
-- or --
HTTP 400 Bad Request
Please pass a name on the query string or in the request body
Common Pitfalls
- Forgetting to install the
azure-functionspackage in your environment causes import errors. - Not setting the correct trigger type or authorization level in
function.jsoncan block function execution. - Missing the
mainfunction or wrong function signature leads to runtime errors. - Not deploying all required files (
function.json,__init__.py, and dependencies) causes failures.
python
Wrong function signature example: def main(): return "Hello" Correct function signature: def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: # function code here
Quick Reference
| Step | Description |
|---|---|
| Create Function App | Use Azure Portal or CLI to create a Python function app. |
| Write Code | Create __init__.py with your Python function. |
| Define Trigger | Set trigger and bindings in function.json. |
| Install Dependencies | Add requirements.txt with needed packages. |
| Deploy | Use Azure CLI or VS Code to deploy your function. |
| Test | Invoke function via HTTP or other trigger to verify. |
Key Takeaways
Azure Functions run Python code triggered by events like HTTP requests.
Define triggers in function.json and write your Python logic in __init__.py.
Always install azure-functions package and match the function signature.
Deploy all function files and dependencies to Azure for the function to work.
Test your function locally and in Azure to ensure it responds correctly.