Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered
I'm running into an issue trying to connect to a Lakehouse SQL endpoint using pyodbc and a service principal in Python. Despite verifying that the service principal works (I successfully authenticated using SSMS with the same credentials), I get the following error in my script:
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
Using Python with pyodbc
Target: Lakehouse SQL endpoint
Authentication: Service Principal (Client ID + Secret)
Verified network/firewall is not an issue (connection via SSMS using the same service principal works fine)
Using ODBC Driver 18
Tried setting ConnectRetryCount to handle retries
Here's a redacted version of my code:
import pyodbc
TENANT_ID = "<tenant-id>"
CLIENT_ID = "<client-id>"
CLIENT_SECRET = "<client-secret>"
server = "<sql-endpoint>.datawarehouse.fabric.microsoft.com"
database = "<database-name>"
username = CLIENT_ID
password = CLIENT_SECRET
connection_string = f"Driver={{ODBC Driver 18 for SQL Server}};Server={server};Database={database};Authentication=ActiveDirectoryServicePrincipal;UID={username};PWD={password};ConnectRetryCount=4"
print("Attempting connection...")
conn = pyodbc.connect(connection_string) # Fails here with login timeout
# Sample query
cursor = conn.cursor()
cursor.execute("SELECT * FROM sys.tables")
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
Solved! Go to Solution.
Hi @ThucLe ,
Thanks for reaching out to the Microsoft fabric community forum.
@BhavinVyas3003
Thanks for your prompt response
@ThucLe
You must include Encrypt=yes;TrustServerCertificate=no; in your connection string. Without those, the ODBC driver silently fails during the Azure AD auth handshake, resulting in a timeout.
import pyodbc
TENANT_ID = "<tenant-id>"
CLIENT_ID = "<client-id>"
CLIENT_SECRET = "<client-secret>"
server = "<lakehouse-name>.datawarehouse.fabric.microsoft.com"
database = "<your-database-name>"
username = CLIENT_ID
password = CLIENT_SECRET
connection_string = (
"Driver={ODBC Driver 18 for SQL Server};"
f"Server={server};"
f"Database={database};"
"Authentication=ActiveDirectoryServicePrincipal;"
f"UID={username};"
f"PWD={password};"
"Encrypt=yes;"
"TrustServerCertificate=no;"
"ConnectRetryCount=4;"
)
print("Attempting connection...")
conn = pyodbc.connect(connection_string, timeout=30)
cursor = conn.cursor()
cursor.execute("SELECT * FROM sys.tables")
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
Additional Notes:
Make sure ODBC Driver 18 is installed.
Double-check that the Lakehouse SQL endpoint is correct.
You don't need to explicitly specify the TenantId in the connection string — Authentication=ActiveDirectoryServicePrincipal handles that based on the domain.
Using Microsoft Entra ID with the ODBC Driver - ODBC Driver for SQL Server | Microsoft Learn
Python SQL Driver - pyodbc - Python driver for SQL Server | Microsoft Learn
If this post helped resolve your issue, please consider giving it Kudos and marking it as the Accepted Solution. This not only acknowledges the support provided but also helps other community members find relevant solutions more easily.
We appreciate your engagement and thank you for being an active part of the community.
Best regards,
LakshmiNarayana.
Hi @ThucLe ,
Thanks for reaching out to the Microsoft fabric community forum.
@BhavinVyas3003
Thanks for your prompt response
@ThucLe
You must include Encrypt=yes;TrustServerCertificate=no; in your connection string. Without those, the ODBC driver silently fails during the Azure AD auth handshake, resulting in a timeout.
import pyodbc
TENANT_ID = "<tenant-id>"
CLIENT_ID = "<client-id>"
CLIENT_SECRET = "<client-secret>"
server = "<lakehouse-name>.datawarehouse.fabric.microsoft.com"
database = "<your-database-name>"
username = CLIENT_ID
password = CLIENT_SECRET
connection_string = (
"Driver={ODBC Driver 18 for SQL Server};"
f"Server={server};"
f"Database={database};"
"Authentication=ActiveDirectoryServicePrincipal;"
f"UID={username};"
f"PWD={password};"
"Encrypt=yes;"
"TrustServerCertificate=no;"
"ConnectRetryCount=4;"
)
print("Attempting connection...")
conn = pyodbc.connect(connection_string, timeout=30)
cursor = conn.cursor()
cursor.execute("SELECT * FROM sys.tables")
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
Additional Notes:
Make sure ODBC Driver 18 is installed.
Double-check that the Lakehouse SQL endpoint is correct.
You don't need to explicitly specify the TenantId in the connection string — Authentication=ActiveDirectoryServicePrincipal handles that based on the domain.
Using Microsoft Entra ID with the ODBC Driver - ODBC Driver for SQL Server | Microsoft Learn
Python SQL Driver - pyodbc - Python driver for SQL Server | Microsoft Learn
If this post helped resolve your issue, please consider giving it Kudos and marking it as the Accepted Solution. This not only acknowledges the support provided but also helps other community members find relevant solutions more easily.
We appreciate your engagement and thank you for being an active part of the community.
Best regards,
LakshmiNarayana.
Hi @ThucLe ,
If your issue has been resolved, please consider marking the most helpful reply as the accepted solution. This helps other community members who may encounter the same issue to find answers more efficiently.
If you're still facing challenges, feel free to let us know we’ll be glad to assist you further.
Looking forward to your response.
Best regards,
LakshmiNarayana.
Hi @ThucLe ,
If your question has been answered, kindly mark the appropriate response as the Accepted Solution. This small step goes a long way in helping others with similar issues.
We appreciate your collaboration and support!
Best regards,
LakshmiNarayana
Try to include Authority with your Tenant ID
Authority=https://login.microsoftonline.com/<tenant-id> is required for service principal auth to work.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Fabric update to learn about new features.
User | Count |
---|---|
3 | |
1 | |
1 | |
1 | |
1 |