python-3.xvisual-studio-codepyodbcmicrosoft-fabric

Fabric lakehouse - Python - pyodbc - connection error


I attempted to connect to Fabric Lakehouse using python in VS code as per the code below. I have visited on a lot of other sources related to '08001' error, but this problem keeps coming up.

server = 'tcp:myserver.database.windows.net'. # fabric lakehouse sql endpoint
database = 'mydb'
username = 'myusername'
pass1 = 'xxxx'
auth = 'ActiveDirectoryPassword'

cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';Authentication='+auth+';UID='+username+';PWD='+pass1+';ENCRYPT=yes;Trusted_Connection=yes')

cursor = cnxn.cursor()

Error Message:

" OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) "

Any help would be greatly appreciated.


Solution

  • OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) 
    

    When the network is not allowing to connect to the server then you may get above error. Make sure your network is allowed to connect the server. Along with that according to the MS document

    Only Microsoft Entra multifactor authentication (MFA) is supported.

    So, when you are connecting to the server use ActiveDirectoryInteractive option instead of ActiveDirectoryPassword for authentication as mentioned in the below code:

    import pyodbc
    server = 'tcp:<serverName>.database.windows.net' # fabric lakehouse sqlendpoint
    database = '<dbName>'
    username = '<v-id>'
    auth = 'ActiveDirectoryInteractive'
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';Authentication='+auth+';UID='+username+';ENCRYPT=yes;')
    cursor = cnxn.cursor() 
    

    when run the above code new window will open for authentication, complete the authentication, then you will be able to connect it successfully as shown below:

    enter image description here

    For more information you can refer to the MS document.