'Using pyodbc to run sql query but get Error: "Data source name not found and no default driver specified"

I have this running in an aws lambda function to run sql queries in a MSSQL database. The pyodbc connection string i'm using is:

    cnxn = pyodbc.connect(
    server="SERVER NAME",
    database="DATABASE NAME",
    user=sql_username,
    password=sql_password,
    port=1433,
    driver='{ODBC Driver 17 for SQL Server}'
    )

Which gives me error:

[ERROR] InterfaceError: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)')

I have the relevant drivers. In file odbc.ini:

[ODBC Driver 17 for SQL Server]
Driver = ODBC Driver 17 for SQL Server
Description = My ODBC Driver 17 for SQL Server
Trace = No 

In file odbcinst.ini:

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1
UsageCount=1

And that is the correct path to the driver.

I have noticed there is a "ODBCDataSources" folder that came along with the other odbc files but the folder is empty. Should there be something in here?

Or is there something else that would be causing the error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source