14

I am trying to write a generic database connector class in python. For this I want to first check what drivers are installed on the machine and throw an error if the required drivers are missing in the machine.

Is there a way to do this in python?

haraprasadj
  • 1,059
  • 1
  • 8
  • 17

2 Answers2

45

pyodbc has a method which returns a list of installed ODBC drivers. Granted, it's just a list of the driver names, so it's a bit fiddly getting to the most current driver, but hopefully this will help.

I use regex (via the built-in re module) to filter down to the driver I need.

import pyodbc
pyodbc.drivers()

The output provides a list of installed ODBC drivers.

S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • For some reason I am unable to use this method. (Attribute not found error). Tried on Python 3 as well as 2. Am I missing something? – haraprasadj Nov 10 '16 at 12:17
  • FYI - am trying on Mac – haraprasadj Nov 10 '16 at 12:18
  • 1
    This is great. To access the drivers you can grab the most recent one with `driver = sorted(pyodbc.drivers()).pop()` or to grab an earlier version `driver = sorted(pyodbc.drivers(), reverse=True).pop()` – C.Nivs Dec 11 '18 at 17:33
  • 2
    @haraprasadj : perhaps there was a bug in the `pyodbc` package which has since been fixed. On my mac (Python 3.7.3, pyodbc version 4.0.27) that method works. I'd show a code snippet, but the markdown instructions don't actually work. – Bob Kline Aug 25 '19 at 15:01
0

There's nothing built into Python that will allow you to do this, except maybe doing something really ugly with ctypes and I'm not sure if that will work. However, you can probably do it with Tim Golden's wmi.py module.

I've found examples of using WMI to find regular drivers:

So you would probably have to filter the list somehow.

Community
  • 1
  • 1
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88