Search code examples
pythonsql-servergoogle-colaboratorypyodbc

How to enable pyodbc on google colab?


When installing pyodbc on Google Colab I get the following error:

[unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server'

I have installed pyodbc several ways with no luck.

I have tried installing the following libraries as seen in other posts:

!curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
!curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
!sudo apt-get update
!sudo ACCEPT_EULA=Y apt-get -q -y install msodbcsql17

But this fails due to dependency issues


Solution

  • I was able to solve the issue and able to successfully query a SQL Server database with the following code:

    I was missing the odbcinst package:

    !curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    !curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
    !sudo apt-get update
    !sudo ACCEPT_EULA=Y apt-get -q -y install odbcinst
    !sudo ACCEPT_EULA=Y apt-get -q -y install msodbcsql17
    

    I was able to install pyodbc with:

    !pip install pyodbc
    

    And was able to create the connection to a SQL Server database with the following code:

    SERVER = 'server.sample.com,port'
    DATABASE = 'XXXX'
    USERNAME = 'YYYY'
    PASSWORD = 'ZZZZ'
    
    connectionString = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}'
    
    conn = pyodbc.connect(connectionString)