Search code examples
pythonpypyodbc

On Windows with Python and pypyodbc how do you list the drivers available?


For a python script that is shared by many people I need a solution to detect the users driver so I can dynamically construct the connection string. I'm currently using the os environment COMPUTERNAME like:

if os.environ['COMPUTERNAME'] == 'MattsDesktop':
print("You are using: Oracle 12")
    self.DataConnectOracle = 'DRIVER={Oracle in 12cR1client32bit};SERVER=Xyz;DBQ=DEVenv;UID=AllDev;PWD=Secret2Secret'

But this has to be updated whenever a user makes a change. Seems like I should be able to get their driver info and not have to maintain these if's.


Solution

  • While it won't give you the exact version of the driver, pypyodbc.drivers() will give you the name of the drivers available to it on Windows. That should suffice since you only need the driver name (not the version) in order to construct the connection string.

    For example, on my machine

    import pypyodbc
    drivers_list = sorted(pypyodbc.drivers())
    for driver_name in drivers_list:
        print(driver_name)
    

    prints

    Driver da Microsoft para arquivos texto (*.txt; *.csv)
    Driver do Microsoft Access (*.mdb)
    Driver do Microsoft Excel(*.xls)
    Driver do Microsoft Paradox (*.db )
    Driver do Microsoft dBase (*.dbf)
    Driver para o Microsoft Visual FoxPro
    Microsoft Access Driver (*.mdb)
    Microsoft Access Driver (*.mdb, *.accdb)
    Microsoft Access Text Driver (*.txt, *.csv)
    Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)
    Microsoft Access-Treiber (*.mdb)
    Microsoft Excel Driver (*.xls)
    Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)
    Microsoft Excel-Treiber (*.xls)
    Microsoft FoxPro VFP Driver (*.dbf)
    Microsoft ODBC for Oracle
    Microsoft Paradox Driver (*.db )
    Microsoft Paradox-Treiber (*.db )
    Microsoft Text Driver (*.txt; *.csv)
    Microsoft Text-Treiber (*.txt; *.csv)
    Microsoft Visual FoxPro Driver
    Microsoft Visual FoxPro-Treiber
    Microsoft dBase Driver (*.dbf)
    Microsoft dBase VFP Driver (*.dbf)
    Microsoft dBase-Treiber (*.dbf)
    MySQL ODBC 5.3 ANSI Driver
    MySQL ODBC 5.3 Unicode Driver
    ODBC Driver 11 for SQL Server
    SQL Server
    SQL Server Native Client 11.0
    SQLite3 ODBC Driver
    

    For a list of just the Oracle drivers, I can use

    oracle_list = [x for x in pypyodbc.drivers() if "Oracle" in x]