Python ODBC (pyodbc) connection to MSSQL server fails with error "Can’t open lib … file not found"- but driver file is there

  odbc, pyodbc, python-3.x, sql-server

I am trying to get pyodbc to connect to SQL Server with this test script:

import pyodbc
server = 'ms.sql.server'
database = 'database'
username = 'username'
password = 'password'

drivers = [item for item in pyodbc.drivers()]
driver = drivers[-1]

cnxn = pyodbc.connect('DRIVER={'+driver+'};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

#Sample select query
cursor.execute("SELECT loginame FROM sys.sysprocesses  WHERE spid = @@SPID")
row = cursor.fetchone()
while row:
    row = cursor.fetchone()

The error output looks like:

['ODBC Driver 17 for SQL Server']
driver:ODBC Driver 17 for SQL Server
Traceback (most recent call last):
  File "", line 12, in <module>
    cnxn = pyodbc.connect('DRIVER={' + driver + '};SERVER='+server+';DATABASE='+database)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql17/lib64/' : file not found (0) (SQLDriverConnect)")

The driver file it is complaining about exists:

[[email protected]]# ll /opt/microsoft/msodbcsql17/lib64/
-rwxr-xr-x. 1 root root 2065280 Mar  5 08:28 /opt/microsoft/msodbcsql17/lib64/

I am working on Centos and installed msodbcsql17 with

curl > /etc/yum.repos.d/mssql-release.repo
yum install msodbcsql17

Not sure if it’s relevant, but I also installed mssql-tools (bcp/sqlcmd) and they are working correctly.

After trawling through similar-but-different posts on stackoverflow I had no luck finding a solution. Below are checks I saw on other posts that might be helpful with diagnosis.

Output from odbcinst -j :

unixODBC 2.3.9
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8

Contents of /etc/odbcinst.ini :

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server

ldd output:

ldd /opt/microsoft/msodbcsql17/lib64/ (0x00007ffef0bf3000) => /lib64/ (0x00007f1678da1000) => /lib64/ (0x00007f1678b99000) => /lib64/ (0x00007f167897f000) => /lib64/ (0x00007f1678696000) => /lib64/ (0x00007f1678441000) => /lib64/ (0x00007f16780ac000) => /lib64/ (0x00007f1677d2a000) => /lib64/ (0x00007f1677b12000) => /lib64/ (0x00007f16778f2000) => /lib64/ (0x00007f167752f000)
        /lib64/ (0x00007f16793b1000) => /lib64/ (0x00007f1677318000) => /lib64/ (0x00007f1677114000) => /lib64/ (0x00007f1676f03000) => /lib64/ (0x00007f1676cff000) => /lib64/ (0x00007f1676819000) => /lib64/ (0x00007f1676602000) => /lib64/ (0x00007f16763d8000) => /lib64/ (0x00007f16761c1000) => /lib64/ (0x00007f1675f3d000)

Any idea what the problem is here?

Source: Python-3x Questions

One Reply to “Python ODBC (pyodbc) connection to MSSQL server fails with error "Can’t open lib … file not found"- but driver file is there”

Leave a Reply to Lucie ouvrier-buffet Cancel reply