grand2019
grand2019

Reputation: 632

Can't open lib 'SQL Server Native Client 11.0' when running python3 file in Ubuntu VM

I am trying to run my python code within a Ubunu VM (18.04.3) but keep getting the error:

Traceback (most recent call last): File "python-script.py", line 33, in conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};' pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server Native Client 11.0' : file not found (0) (SQLDriverConnect)")

I have installed pyodbc with no errors so am confused as to what could be causing this issue. I have done lots of search to a solution but most are for different driver versions and do not resolve this issue. Below is my code for connecting to the DB which I believe could be causing this error:

conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
                      'Server=servername;'
                      'Database=databasename;'
                      'Trusted_Connection=yes;')

Any help with this would be much appreciated!

Upvotes: 2

Views: 3009

Answers (2)

Casivio
Casivio

Reputation: 363

You can get the drivers installed from pyodbc

drivers = [item for item in pyodbc.drivers()]

Then I loop through it to make sure that it matches what I need

for driver in drivers:
    print(driver)
    # Check for correct driver 
    if 'ODBC' in driver and 'SQL Server' in driver:
        useDriver = driver 

    

Then it is ready to put into the connection

db = pyodbc.connect(driver=useDriver, 
                        server=server, 
                        database=database,
                        user=username, 
                        password=password)

Upvotes: 1

Gord Thompson
Gord Thompson

Reputation: 123654

After you install the ODBC driver according to the instructions at

https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

you need to use

DRIVER=ODBC Driver 17 for SQL Server;...

Upvotes: 4

Related Questions