I have a simple connection string for connection to a MSSQL server running in a local docker container. The code works fine in python 3.7 (not conda), but fails in any conda environment. I'm specifically interested in getting it to work in a Jupyter notebook.
I've recently re-installed Anaconda, but that seems unrelated as it still isn't working. My .bash_profile seems to be a bit of a mess, but I've been reluctant to make changes there before I'm sure about what I'm doing.
As best I can tell, my conda version of python can't find the pyodbc drivers that the other version of python is using.
this code works in any non-conda version of Python for me but fails elsewhere.
import pyodbc
drivers = [item for item in pyodbc.drivers()]
driver = drivers[-1]
print("driver:{}".format(driver))
server = '192.168.0.4'
database = 'XXXXXXX'
uid = 'sa'
pwd = 'XXXXXXXXXX'
con_string = f'DRIVER={driver};SERVER={server};PORT=1433;DATABASE={database};UID={uid};PWD={pwd}'
print(con_string)
cnxn = pyodbc.connect(con_string)
cursor = cnxn.cursor()
print('connected')
cursor.execute('SELECT * FROM TB_STYLE_AUDIT')
for row in cursor:
print(row)
expected results (shortened):
/usr/local/bin/python3.7 /Users/mycomputer/Documents/Pythonprojects/BuildingOldHistory/getHistoricaldata.py
driver:ODBC Driver 17 for SQL Server
DRIVER=ODBC Driver 17 for SQL Server;SERVER=192.168.0.4;PORT=1433;DATABASE=XXXXXXX;UID=sa;PWD=XXXXXXX
connected
(37962, 107, 555255, 662895, 689233, datetime.datetime(2016, 6, 8, 13, 22, 38), 1, '', '', '', -1.0, -10.0, -20.0, 'Sale', '1086504', '1088527', None, 18, None)
here is the error from conda:
IndexError Traceback (most recent call last)
<ipython-input-1-ea3728340faa> in <module>
2
3 drivers = [item for item in pyodbc.drivers()]
----> 4 driver = drivers[-1]
5 print("driver:{}".format(driver))
6 server = '192.168.0.4'
IndexError: list index out of range
Editing to add that this code also works outside of conda:
import pyodbc
#drivers = [item for item in pyodbc.drivers()]
#driver = drivers[-1]
#print("driver:{}".format(driver))
driver = 'ODBC Driver 17 for SQL Server'
server = '192.168.0.4'
database = 'XXXXXXX'
uid = 'sa'
pwd = 'XXXXXXX'
con_string = f'DRIVER={driver};SERVER={server};PORT=1433;DATABASE={database};UID={uid};PWD={pwd}'
print(con_string)
cnxn = pyodbc.connect(con_string)
cursor = cnxn.cursor()
print('connected')
cursor.execute('SELECT * FROM TB_STYLE_AUDIT')
for row in cursor:
print(row)
but produces this error in a notebook:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-2-3f7d96055440> in <module>
11 con_string = f'DRIVER={driver};SERVER={server};PORT=1433;DATABASE={database};UID={uid};PWD={pwd}'
12 print(con_string)
---> 13 cnxn = pyodbc.connect(con_string)
14
15
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")