Note: Reasons for failure seem to vary a lot over time given version of macOS, Pip, Homebrew, and unixODBC.
Running pip install pyodbc
while specifying the location of unixODBC library works in pyodbc 4.0.32:
CPPFLAGS='-I/opt/homebrew/Cellar/unixodbc/2.3.9_1/include' \
LDFLAGS='-L/opt/homebrew/Cellar/unixodbc/2.3.9_1/lib' \
pip install pyodbc
There seems not to be need to install iODBC as unixODBC works.
Additionally, getting ODBC Driver for SQL Server is as tangled. Documentation at https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos isn't accurate as OpenSSL version 3 and 1 fails to correctly detect at install time.
Because of OpenSSL version conflict, connection attempts to SQL server will result in errors like:
- SSL Provider: [OpenSSL library could not be loaded, make sure OpenSSL 1.0 or 1.1 is installed].
- Client unable to establish the connection.
- WARNING: <command/library here> is loading libcrypto in an unsafe way (followed by a process crash)
Various attempts to fix driver loading are described in https://github.com/microsoft/homebrew-mssql-release/issues/59.
Manually symlinking OpenSSL 1.1 fixes the problem at the time of writing:
ln -s /opt/homebrew/opt/openssl@1.1/lib/libssl.dylib /usr/local/lib/
ln -s /opt/homebrew/opt/openssl@1.1/lib/libcrypto.dylib /usr/local/lib/
Please understand, this is subject to change.