Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
ThucLe
New Member

pyodbc login timeout expired when connecting to Lakehouse SQL endpoint using Service Principal

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)')

 

Environment & What I've Tried:

  • 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()

 

 

1 ACCEPTED SOLUTION
v-lgarikapat
Community Support
Community Support

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
.

View solution in original post

6 REPLIES 6
v-lgarikapat
Community Support
Community Support

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

Hi @v-lgarikapat 

You said:

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.

Is this still true now (about a year later)?

I'm also having timeout messages when trying to connect, and it seems possible that your suggestion is the reason for the problems.

Your explanation sounds so crazy, that I am starting to think it might be true.  Can you share any documentation about this silent suppression of auth failures?  How did you learn about it?  Can you please share a source?  I will probably open a support ticket with CSS-pro at Mindtree.  Do you have a prior SR# or ICM# that can be used as a reference?  The MT engineers will probably have a hard time believing your explanation without some sort of corroboration.

Hi @dbeavon3 ,

 

Thanks for reaching out to the Fabric forum. Since this thread has already been marked as solved, could you please create a new thread that covers your specific use cases along with sample details? This will help us address your follow‑up questions more effectively

 

 

Thanks,

Lakshmi.

BhavinVyas3003
Super User
Super User

Try to include Authority with your Tenant ID

Authority=https://login.microsoftonline.com/<tenant-id> is required for service principal auth to work.


Thanks,
Bhavin
Problem solved? Hit “Accept as Solution” and high-five me with a Kudos! Others will thank you later!

Helpful resources

Announcements
FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Fabric Update Carousel

Fabric Monthly Update - March 2026

Check out the March 2026 Fabric update to learn about new features.