pyODBC refuses to look for unixODBC, instead looks for iODBC

半世苍凉 提交于 2019-12-12 02:33:47

问题


Is there a way to get pyODBC v3.0.10 to look for the unixODBC driver, instead of the iODBC driver it seems to want to look for?

My understanding is that pyODBC v3.0.10 is supposed to do this by default, while versions prior to v3.0.7 required a manual edit to the setup.py file (see reference here).

One more clue, I ran this code to list my ODBC sources, and it returned nothing:

sources = pyodbc.dataSources()
dsns = list(sources.keys())
dsns.sort()
sl = []
for dsn in dsns:
    sl.append('%s [%s]' % (dsn, sources[dsn]))
print('\n'.join(sl))

Further Background

I have been struggling with creating a connection to MSSQL Server using the following setup: pyODBC --> unixODBC --> FreeTDS --> MS SQL. The gory details are documented here.

I've got it narrowed to a specific issue (I think): the pyODBC package is looking for the iODBC driver instead of the unixODBC driver I've installed and configured. I believe this because when I run:

import pyodbc

pyodbc.connect(
    'DRIVER=FreeTDS;'
    'SERVER=MyServerIP;'
    'PORT=1433;'
    'DATABASE= DatabaseName;'
    'UID=MyUsername;'
    'PWD=MyPassword')

I get this error, with a reference to not finding the iODBC driver:

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-12-607f0d66e615> in <module>()
      1 pyodbc.connect(
----> 2     'DRIVER=FreeTDS;'
      3     'SERVER= MyServerIP;'
      4     'PORT=1433;'
      5     'DATABASE= DatabaseName;'

Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen(FreeTDS, 6): image not found (0) (SQLDriverConnect)')

Thanks for any light you can shed.


回答1:


I believe the reason is you may have compiled pyodbc to use iODBC (or maybe that's the default). If you want tomake sure you build pyodbc against unixODBC, you can specify that just before running setup.py build, as instructed here (look at http://www.easysoft.com/developer/languages/python/pyodbc.html , headline pyodbc 3.0.x). Alternatively, try to follow the instructions here: https://code.google.com/archive/p/pyodbc/wikis/Building.wiki




回答2:


This is a repost of my answer here, but this answers this, more-specific formation of the question. Hope I'm not violating any rules, but this took me several weeks to figure out, and a lot of peoples' help. So if it's a duplicate Q&A, so be it -- we earned it.

Well, we solved it -- with the help of a lot of people on this page and the original, chasing down a lot of blind alleys.

As (eventually) suspected, it was the pyodbc link in the connection. I was using pyodbc v3.0.10, by downloading from the Anaconda package repository. The solution was v.3.0.9. Once I uninstalled v3.0.10, downloaded v3.0.9 from the pypi repository and then built and installed my own conda package... it worked.

The steps I took were as follows (note these were specific to an anaconda environment):

conda uninstall pyodbc

conda skeleton pypi pyodbc --version 3.0.9

conda build pyodbc

conda install pyodbc=3.0.9 --use-local

Once I went back to my Jupyter notebook and ran the same code above, it created a good connection.

I do not know what is wrong with v.3.0.10, or if it's just the files that anaconda.org has on their repository. I've posted something on the pyodbc github page also, but it doesn't look that active.

Anyway, thank you for everyone's help. I hope this saves someone some time.



来源:https://stackoverflow.com/questions/37947482/pyodbc-refuses-to-look-for-unixodbc-instead-looks-for-iodbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!