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
@rosiecakes
Copy link
Author

After upgrading python 3.5.2 to 3.5.3, had to reinstall pips, pymssql failed to no surprise.

Tried same as above, tried brew doctor, brew update, brew unlink/link freetds, freetds 1.00 (didn't work, back to 091), tried steps here: https://groups.google.com/forum/#!topic/pymssql/Z0HuJpJGAfo

Didn't work. Edited the hell out of setup.py since it was obvious it was a path/include folders issue.

It worked after changing whatever fink is supposed to be from '/sw' to the path I knew sqlfront.h was in:

fink = '/Users/shimadar/usr/local'

@rdelduca
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

@rosiecakes
Copy link
Author

This was just for personal reference but in case someone else comes by, I had FreeTDS and unixodbc installed via brew as outlined here already https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX, and still ran into this issue.

The above fix however didn't work. I was able to use tsql but not isql, and could not import pyodbc.

Was able to import pyodbc in a Python shell after running:
$ odbcinst -i -d -f /Users/shimadar/usr/local/Cellar/unixodbc/2.3.4/etc/odbcinst.ini

odbcinst: Driver installed. Usage count increased to 1.

But still no isql. Found out I was editing config files in /Users//usr/local/Cellar/unixodbc/2.3.4/etc, but needed to be editing files in /Users//usr/local/etc. Super confusing with Cellar because you don't know which folder you should be in. After adding the freetds.conf, odbc.ini and odbcinst.ini in that directory, isql works.

@psHA5
Copy link

psHA5 commented May 5, 2020

Using Pycharm and virtualenv on a Mac High Sierra, I get the error:

import pyodbc

ImportError: dlopen(/Users/peter/PycharmProjects/web2020/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/peter/PycharmProjects/web2020/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: image not found

@serbi
Copy link

serbi commented May 6, 2020

@psHA5 have you found any solution?

@rosiecakes
Copy link
Author

did you try the install_name_tool mentioned in my post? that has fixed this issue throughout several updates for me.

@serbi
Copy link

serbi commented May 6, 2020

I figured out I was missing the brew install unixodbc. This solved my issue. Thanks for your help @rosiecakes

@psHA5
Copy link

psHA5 commented May 6, 2020

brew install unixodbc - this worked for me as well, fantastic! Thank you all so much!

@taylorbond
Copy link

Thanks @rdelduca - brew install unixodbc worked beautifully.

@Speccles96
Copy link

brew install unixodbc fixed the issue for me

@NathanDotTo
Copy link

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.

@Jelena11
Copy link

Jelena11 commented Nov 1, 2020

Thanks @rdelduca, brew install unixodbc also worked for me.

@dung38tn
Copy link

dung38tn commented Dec 9, 2020

awesome. thanks, @rdelduca, brew install unixodbc worked for me

@inap-bannai
Copy link

brew install unixodbc works !!

@RafaAguilar
Copy link

brew install unixodbc was the way to go for me, thanks @serbi.

@DigitalDuquette
Copy link

Just another comment here that yeah, brew install unixodbc does get me past the error in import pyodbc.

🤷🏻‍♂️

@RohitShende
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

@Ritish-Madan
Copy link

I did install the unixODBC using Homebrew. And I have the Python installed using the Graphic Installer, instead of Homebrew installation of Python.

I don't know if it is creating the problem, but installing the unixodbc is still throwing the same error after importing pyodbc

I did also tried using brew install python3, but Homebrew version of python do not install the other libraries like Pandas, Numpy, Pyodbc too. So I installed the Python3 graphically and got all the other libraries installed.

I have MacBook Air M1

@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

@mynenic
Copy link

mynenic commented Jan 4, 2025

brew install unixodbc

This did help me

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