Error: `pyodbc.drivers()` returns empty list inside VSCode

I followed Microsoft instructions for installing its ODBC driver and prepared a conda environment with the necessary packages to work with azure SQL database with Python, including pyodbc. I used the instructions for Ubuntu because I'm on Linux Mint 20 and well, that option isn't available (and Linux Mint is based on Ubuntu).

Then I prepared a small script to connect to a test azure database that I created:

import pyodbc

configString = (
    f'DRIVER={ODBC Driver 17 for SQL Server};'

with pyodbc.connect(configString) as conn:
    with conn.cursor() as cursor:
        cursor.execute('SELECT TOP 3 name, collation_name FROM sys.databases')
        row = cursor.fetchone()
        while row:
            print (str(row[0]) + ' ' + str(row[1]))
            row = cursor.fetchone()

This fails with the error ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)").
But the file exists, and if I run this from just a terminal (instead of from within VSCode) it works fine.

Also, pyodbc.drivers() resturns [] from within VSCode but it returns the correct value ['ODBC Driver 17 for SQL Server'] when called from a Python shell within a terminal.

So the question is: why pyodbc fails to get the drivers from within VSCode but gets the correct values from a terminal?

Read more here:

Content Attribution

This content was originally published by khawabonga at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: