You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: azure-sql/database/azure-sql-python-quickstart.md
+42-8Lines changed: 42 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,10 +25,10 @@ This quickstart describes how to connect an application to a database in Azure S
25
25
## Prerequisites
26
26
27
27
- An [Azure subscription](https://azure.microsoft.com/pricing/purchase-options/azure-account?icid=azurefreeaccountpython/).
28
-
- An Azure SQL database configured with Microsoft Entra authentication. You can create one using the [Quickstart: Create a single database - Azure SQL Database](single-database-create-quickstart.md).
28
+
- An Azure SQL database configured with Microsoft Entra authentication. You can create one using the [Quickstart: Create a single database - Azure SQL Database](single-database-create-quickstart.md). Alternatively, you can use a [SQL database in Microsoft Fabric](/fabric/database/sql/create).
29
29
- Visual Studio Code with the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python).
30
30
- Python 3.10 or later.
31
-
- The [Azure CLI](/cli/azure/install-azure-cli)(required for `ActiveDirectoryDefault` authentication).
31
+
- The [Azure CLI](/cli/azure/install-azure-cli) for passwordless authentication (works on Windows, macOS, and Linux).
32
32
33
33
## Configure the database
34
34
@@ -69,7 +69,8 @@ Create a new Python project using Visual Studio Code.
69
69
70
70
To connect to Azure SQL Database using Python, install the `mssql-python` driver. This driver has built-in support for Microsoft Entra authentication, eliminating the need for manual token handling. In this quickstart, you also install `fastapi`, `uvicorn`, and `pydantic` packages to create and run an API.
71
71
72
-
For details and specific instructions for installing the `mssql-python` driver, see [mssql-python installation guide](https://github.com/microsoft/mssql-python/wiki/Installation).
72
+
> [!NOTE]
73
+
> On macOS and Linux, system dependencies are required before installing `mssql-python`. See [Install the mssql-python package](/sql/connect/python/mssql-python/python-sql-driver-mssql-python-quickstart#install-the-mssql-python-package) for platform-specific instructions.
73
74
74
75
1. Create a *requirements.txt* file with the following lines:
75
76
@@ -97,16 +98,31 @@ For local development, create a `.env` file in your project folder to store your
97
98
98
99
The mssql-python driver has built-in support for Microsoft Entra authentication. Use the `Authentication` parameter to specify the authentication method.
`ActiveDirectoryDefault` automatically discovers credentials from multiple sources (Visual Studio Code, Azure CLI, environment variables, etc.) without requiring interactive login. This method is convenient for local development but slower due to credential discovery.
103
+
`ActiveDirectoryDefault` automatically discovers credentials from multiple sources without requiring interactive login. This is the **recommended option for local development**.
104
+
105
+
For the most reliable local development experience, sign in with Azure CLI first:
106
+
107
+
```bash
108
+
az login
109
+
```
110
+
111
+
Then use this connection string format in your `.env` file:
> `ActiveDirectoryDefault` is intended for local development only. It tries multiple authentication methods in sequence (environment variables, managed identity, Azure CLI, Visual Studio, etc.), which adds latency. For production applications, use the specific authentication method for your scenario:
117
+
`ActiveDirectoryDefault` evaluates credentials in the following order:
118
+
1.**Environment variables** (for service principal credentials)
119
+
1.**Managed identity** (when running on Azure)
120
+
1.**Azure CLI** (from `az login`)
121
+
1.**Visual Studio** (Windows only)
122
+
1.**Azure PowerShell** (from `Connect-AzAccount`)
123
+
124
+
> [!TIP]
125
+
> For production applications, use the specific authentication method for your scenario to avoid credential discovery latency:
110
126
> -**Azure App Service/Functions**: Use `ActiveDirectoryMSI` (managed identity)
111
127
> -**Interactive user login**: Use `ActiveDirectoryInteractive`
112
128
> -**Service principal**: Use `ActiveDirectoryServicePrincipal`
> Use caution when managing connection strings that contain secrets such as usernames, passwords, or access keys. These secrets shouldn't be committed to source control or placed in unsecure locations where they might be accessed by unintended users. Add `.env` to your `.gitignore` file to prevent accidentally committing secrets.
132
148
149
+
## [Fabric SQL Database](#tab/sql-fabric)
150
+
151
+
To connect to a [SQL database in Microsoft Fabric](/fabric/database/sql/overview), use the same authentication methods. The server name follows the Fabric format.
152
+
153
+
On **Windows domain-joined machines**, use `ActiveDirectoryIntegrated` for seamless authentication with no extra steps:
Copy file name to clipboardExpand all lines: azure-sql/database/develop-kubernetes-application.md
+32-29Lines changed: 32 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@ titleSuffix: Azure SQL Database
4
4
description: Learn how to develop a modern application using Python, Docker Containers, Kubernetes, and Azure SQL Database.
5
5
author: rwestMSFT
6
6
ms.author: randolphwest
7
-
ms.reviewer: wiassaf, damauri, mathoma
8
-
ms.date: 06/13/2025
7
+
ms.reviewer: wiassaf, damauri, mathoma, dlevy
8
+
ms.date: 02/03/2026
9
9
ms.service: azure-sql-database
10
10
ms.subservice: development
11
11
ms.topic: tutorial
@@ -65,15 +65,14 @@ Throughout this article, there are several values you should replace. Ensure tha
65
65
-`ReplaceWith_AzureContainerRegistryName`: Replace this value with the name of the Azure Container Registry you would like to create.
66
66
-`ReplaceWith_AzureKubernetesServiceName`: Replace this value with the name of the Azure Kubernetes Service you would like to create.
67
67
68
-
The developers at AdventureWorks use a mix of Windows, Linux, and Apple systems for development, so they are using [Visual Studio Code](https://code.visualstudio.com/Download) as their environment and git for the source control, both of which run cross-platform.
68
+
The developers at AdventureWorks use a mix of Windows, Linux, and Apple systems for development, so they're using [Visual Studio Code](https://code.visualstudio.com/Download) as their environment and git for the source control, both of which run cross-platform.
69
69
70
70
For the PoC, the team requires the following prerequisites:
71
71
72
-
1.**Python, pip, and packages** - The development team chooses the [Python programming language](/training/paths/beginner-python/) as the standard for this web-based application. Currently they are using version 3.9, but any version supporting the PoC required packages is acceptable.
73
-
- You can [download Python version 3.9](https://www.python.org/downloads/release/python-390/) from python.org.
74
-
1. The team is using the `pyodbc` package for database access.
75
-
- You can [install the pyodbc package with *pip* commands](https://pypi.org/project/pyodbc/).
76
-
- You might also need the [Microsoft ODBC Driver software](/sql/connect/odbc/download-odbc-driver-for-sql-server?view=azuresqldb-current&preserve-view=true) if you do not have it installed already.
72
+
1.**Python, pip, and packages** - The development team chooses the [Python programming language](/training/paths/beginner-python/) as the standard for this web-based application. Currently they're using version 3.10, but any version supporting the PoC required packages is acceptable.
73
+
- You can [download Python version 3.10](https://www.python.org/downloads/) from python.org.
74
+
1. The team is using the `mssql-python` driver for database access.
75
+
- You can [install the mssql-python driver with *pip* commands](https://pypi.org/project/mssql-python/).
77
76
1. The team is using the `ConfigParser` package for controlling and setting configuration variables.
78
77
- You can [install the configparser package with *pip* commands](https://pypi.org/project/configparser/).
79
78
1. The team is using the *Flask package* for a web interface for the application.
@@ -151,20 +150,21 @@ Next, the Development team created a simple Python application that opens a conn
151
150
1. Set up the libraries for the configuration and base web interfaces.
152
151
1. Load the variables from the `.env` file.
153
152
1. Create the Flask-RESTful Application.
154
-
1. Get to Azure SQL Database connection information using the `config.ini` file values.
155
-
1. Create connection to Azure SQL Database using the `config.ini` file values.
156
-
1. Connect to Azure SQL Database using the `pyodbc` package.
153
+
1. Get to Azure SQL Database connection information using the `.env` file values.
154
+
1. Connect to Azure SQL Database using the `mssql-python` driver.
157
155
1. Create the SQL query to run against the database.
158
156
1. Create the class used to return the data from the API.
159
157
1. Set the API endpoint to the `Products` class.
160
158
1. Finally, start the app on default Flask port 5000.
161
159
162
160
```python
163
161
# Set up the libraries for the configuration and base web interfaces
162
+
import os
163
+
import json
164
164
from dotenv import load_dotenv
165
165
from flask import Flask
166
166
from flask_restful import Resource, Api
167
-
importpyodbc
167
+
from mssql_python importconnect
168
168
169
169
# Load the variables from the .env file
170
170
load_dotenv()
@@ -173,22 +173,17 @@ Next, the Development team created a simple Python application that opens a conn
173
173
app = Flask(__name__)
174
174
api = Api(app)
175
175
176
-
# Get to Azure SQL Database connection information using the config.ini file values
176
+
# Get to Azure SQL Database connection information using the .env file values
177
177
server_name = os.getenv('SQL_SERVER_ENDPOINT')
178
178
database_name = os.getenv('SQL_SERVER_DATABASE')
179
179
user_name = os.getenv('SQL_SERVER_USERNAME')
180
180
password = os.getenv('SQL_SERVER_PASSWORD')
181
181
182
-
# Create connection to Azure SQL Database using the config.ini file values
connection = pyodbc.connect(f'Driver=ODBC Driver 17 for SQL Server;Server={ServerName};Database={DatabaseName};uid={UserName};pwd={PasswordValue}')
185
+
# Connect to Azure SQL Database using the mssql-python driver
186
+
connection = connect(connection_string)
192
187
193
188
# Create the SQL query to run against the database
194
189
defquery_db():
@@ -218,7 +213,7 @@ Next, the Development team created a simple Python application that opens a conn
218
213
:::image type="content" source="media/develop-kubernetes-application/api-return-page-localhost-products.png" alt-text="Screenshot from a web browser of the Flask return page." lightbox="media/develop-kubernetes-application/api-return-page-localhost-products.png":::
219
214
220
215
> [!IMPORTANT]
221
-
> When building production applications, do not use the administrator account to access the database. For more information, read more about [how to set up an account for your application](https://devblogs.microsoft.com/azure-sql/create-and-connect-to-an-azure-sql-db/). The code in this article is simplified so that you can quickly get started with applications using Python and Kubernetes in Azure.
216
+
> When building production applications, don't use the administrator account to access the database. For more information, read more about [how to set up an account for your application](https://devblogs.microsoft.com/azure-sql/create-and-connect-to-an-azure-sql-db/). The code in this article is simplified so that you can quickly get started with applications using Python and Kubernetes in Azure.
222
217
>
223
218
> More realistically, you could use a [contained database user](/sql/relational-databases/security/contained-database-users-making-your-database-portable?view=azuresqldb-current&preserve-view=true) with read-only permissions, or a login or contained database user connected to [a user-assigned managed identity](authentication-azure-ad-user-assigned-managed-identity.md) with read-only permissions.
224
219
>
@@ -228,12 +223,11 @@ Next, the Development team created a simple Python application that opens a conn
228
223
229
224
A Container is a reserved, protected space in a computing system that provides isolation and encapsulation. To create a container, use a Manifest file, which is simply a text file describing the binaries and code you wish to contain. Using a Container Runtime (such as Docker), you can then create a binary image that has all of the files you want to run and reference. From there, you can "run" the binary image, the Container, which you can reference as if it were a full computing system. It's a smaller, simpler way to abstract your application runtimes and environment than using a full virtual machine. For more information, see [Containers and Docker](/dotnet/architecture/microservices/container-docker-introduction/docker-defined).
230
225
231
-
The team started with a DockerFile (the Manifest) that layers the elements of what the team wants to use. They start with a base Python image that already has the `pyodbc`
232
-
libraries installed, and then they run all commands necessary to contain the program and config file in the previous step.
226
+
The team started with a DockerFile (the Manifest) that layers the elements of what the team wants to use. They start with a base Python image, and then they run all commands necessary to contain the program and config file in the previous step.
233
227
234
228
The following Dockerfile has the following steps:
235
229
236
-
1. Start with a Container binary that already has Python and `pyodbc` installed.
230
+
1. Start with a Container binary that has Python installed.
237
231
1. Create a Working directory for the application.
238
232
1. Copy all of the code from the current directory into the `WORKDIR`.
239
233
1. Install the libraries that are required.
@@ -242,8 +236,17 @@ The following Dockerfile has the following steps:
242
236
```dockerfile
243
237
# syntax=docker/dockerfile:1
244
238
245
-
# Start with a Container binary that already has Python and pyodbc installed
246
-
FROM laudio/pyodbc
239
+
# Start with a Container binary that has Python installed
- [Connect to and query Azure SQL Database using Python and the pyodbc driver](azure-sql-python-quickstart.md?view=azuresqldb-current&preserve-view=true&tabs=windows%2Csql-inter)
445
+
- [Connect to and query Azure SQL Database using Python and the mssql-python driver](azure-sql-python-quickstart.md?view=azuresqldb-current&preserve-view=true)
443
446
- [What are Dev Container Templates for Azure SQL Database?](local-dev-experience-dev-containers.md)
444
447
- [Browse code samples for Azure SQL Database](/samples/browse/?products=azure-sql-database%2Cazure-sql-managed-instance%2Cazure-sqlserver-vm&expanded=azure)
0 commit comments