How to Deploy Python to Azure App Service Quickly
To deploy Python to Azure App Service, create a web app with a Python runtime using
az webapp create, then push your code using git push or deploy via ZIP. Azure App Service automatically detects Python apps and runs them with the specified runtime.Syntax
Use Azure CLI commands to create and deploy your Python app to Azure App Service.
az webapp create --resource-group <group> --plan <plan> --name <app_name> --runtime <python_version>: Creates the app with Python runtime.git push azure main: Pushes your code to Azure for deployment.az webapp deployment source config-zip --resource-group <group> --name <app_name> --src <zip_file>: Deploys zipped app code.
bash
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myPythonApp --runtime "PYTHON|3.9" git remote add azure https://myPythonApp.scm.azurewebsites.net:443/myPythonApp.git git push azure main
Example
This example shows how to create an Azure App Service for Python 3.9, add Azure as a git remote, and push your app code for deployment.
bash
az group create --name myResourceGroup --location eastus az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myPythonApp --runtime "PYTHON|3.9" --deployment-local-git git remote add azure https://myPythonApp.scm.azurewebsites.net:443/myPythonApp.git git push azure main
Output
Creating resource group 'myResourceGroup'...
Resource group created.
Creating App Service plan 'myAppServicePlan'...
App Service plan created.
Creating web app 'myPythonApp' with Python 3.9 runtime...
Web app created.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Updating branch 'main'.
remote: Syncing repository...
remote: Deployment successful.
To https://myPythonApp.scm.azurewebsites.net:443/myPythonApp.git
abc1234..def5678 main -> main
Common Pitfalls
Common mistakes when deploying Python apps to Azure App Service include:
- Not specifying the correct Python runtime version in
--runtime. - Forgetting to create a Linux App Service plan when deploying Python apps (Python apps require Linux plans).
- Not committing your code before pushing to Azure git remote.
- Missing a
requirements.txtfile for dependencies, causing runtime errors. - Not setting the startup command if your app needs one (like
gunicornfor Flask/Django).
bash
Wrong: az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myPythonApp --runtime "PYTHON|2.7" Right: az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myPythonApp --runtime "PYTHON|3.9"
Quick Reference
Summary tips for deploying Python apps to Azure App Service:
- Use Linux App Service plans for Python apps.
- Specify Python runtime version with
--runtime. - Include
requirements.txtfor dependencies. - Use Azure CLI for creating and deploying apps.
- Push code via Git or deploy ZIP files.
Key Takeaways
Create a Linux App Service plan and specify the Python runtime version when creating the web app.
Deploy your Python code by pushing to the Azure Git remote or by uploading a ZIP file.
Always include a requirements.txt file to install dependencies automatically.
Set a startup command if your app needs one to run correctly on Azure.
Use Azure CLI commands for smooth and repeatable deployment steps.