Skip to content

Instantly share code, notes, and snippets.

@rosiecakes
Created January 19, 2017 14:37
Show Gist options
  • Save rosiecakes/89d3e77c8dded619ed34dc44d25dd33a to your computer and use it in GitHub Desktop.
Save rosiecakes/89d3e77c8dded619ed34dc44d25dd33a to your computer and use it in GitHub Desktop.
mac osx pyodbc stupid dumb annoying issue fix
Homebrew set up to install to /Users/shimadar/usr/local instead of /usr/local
I'm running OSX 10.11.6, Python 3.5.2, pyenv 1.0.6, pyodbc 4.0.1 (via pip).
Went through [this][1] and connect fine with both tsql and isql as shown in the walkthrough.
Still cannot import pyodbc.
>>> import pyodbc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/shimadar/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyodbc.cpython-35m-darwin.so, 2): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/shimadar/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyodbc.cpython-35m-darwin.so
Reason: image not found
Realized lib path slightly different.
$ otool -L /Users/shimadar/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyodbc.cpython-35m-darwin.so
/Users/shimadar/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyodbc.cpython-35m-darwin.so:
/usr/local/opt/unixodbc/lib/libodbc.2.dylib (compatibility version 3.0.0, current version 3.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
Changed the path and now it imports.
$ install_name_tool -change /usr/local/opt/unixodbc/lib/libodbc.2.dylib /Users/shimadar/usr/local/opt/unixodbc/lib/libodbc.2.dylib /Users/shimadar/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pyodbc.cpython-35m-darwin.so
[1]: https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server
[2]: http://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib
@Ritish-Madan
Copy link

Just in case someone else runs across this, I was able to resolve this by installing the unixodbc driver via brew:
brew install unixodbc

Thanks for this it fixed my issue

Which Mac model do you have?

@AmosG
Copy link

AmosG commented Oct 19, 2021

i think there is an issue with the mac m1's
even if you set the path to the

This is a bit of a hack, but ...

sudo mkdir -p  /usr/local/opt/unixodbc
sudo ln -s /usr/local/homebrew/Cellar/unixodbc/2.3.9/lib /usr/local/opt/unixodbc/lib
ls -la   /usr/local/opt/unixodbc/lib 

... /usr/local/opt/unixodbc/lib -> /usr/local/homebrew/Cellar/unixodbc/2.3.9/lib

Voila!

Simply installing unixodbc did not work for me.

as https://gist.github.com/rosiecakes/89d3e77c8dded619ed34dc44d25dd33a#gistcomment-3463218
the error i got was :
Reason: no suitable image found. Did find: /usr/local/opt/unixodbc/lib/libodbc.2.dylib: mach-o, but wrong architecture /usr/local/opt/unixodbc/lib/libodbc.2.dylib: stat() failed with errno=25 /opt/homebrew/Cellar/unixodbc/2.3.9_1/lib/libodbc.2.dylib: mach-o, but wrong architecture

Any ideas on how to proceed ?

@ppromsiri
Copy link

brew install unixodbc. This solved my issue

Thank All

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment