Skip to content

Instantly share code, notes, and snippets.

@mariusdkm
Last active September 12, 2022 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusdkm/a2ec381e7bc1712c29f0ad0b0490671c to your computer and use it in GitHub Desktop.
Save mariusdkm/a2ec381e7bc1712c29f0ad0b0490671c to your computer and use it in GitHub Desktop.
Installing pyodbc using Mac Ports on MacOS (with and without ARM M1 Chip)

I didn't find any tutorial using mac ports to install pyodbc on a mac with m1, so here is my way, maybe it helps somebody. My goal was to talk to a MS SQL Server.

If you want to use Homebrew instead or more information please look here https://gist.github.com/Bouke/10454272

Installing pyodbc

The only dependency you are going to need is freetds, it will install automatically it's dependencies. BUT because we later on need the shared object file libtdsodbc.so you need to install it with as the variant +odbc.

sudo port install freetds +odbc

If everything went smoothly you should now have the following file /opt/local/lib/libtdsodbc.so

Next you can already install pyodbc

# (in your python enviroment)
pip install pyodbc

Using pyodbc

You now have three ways of including the driver in your python program.
Note: When I was trying to connect to a MS SQL Server I had to include the port

Always replace {var} with your value

Just use the path of the library

import pyodbc
pyodbc.connect('DRIVER=/opt/local/lib/libtdsodbc.so;SERVER={IP address};PORT=1433;DATABASE={database};UID={username};PWD={password}')

Using unixODBC

Put the following into /opt/local/etc/odbcinst.ini

[{CONFIG NAME}]
Description = ODBC for FreeTDS
Driver = /opt/local/lib/libtdsodbc.so
Setup = /opt/local/lib/libtdsodbc.so
FileUsage = 1

Then you can use the name you put in as CONFIG NAME in your python program

import pyodbc
pyodbc.connect('DRIVER=CONFIG NAME;SERVER={IP address};PORT=1433;DATABASE={database};UID={username};PWD={password}')

Configure a DSN (Data source name)

  • Modifiy /opt/local/etc/odbcinst.ini as described in "Using unixODBC"
  • Modifiy /opt/local/etc/odbc.ini to include the following:
[{DSN NAME}]
Driver = {CONFIG NAME}
Server = {IP address}
Port = 1433
# You can also include more variables here

Then you can use the name you put in as DSN NAME in your python program

import pyodbc
pyodbc.connect('DSN={DSN NAME};UID={username};PWD={password}')

Other ways of connecting to a MS SQL Server

There is also a port on mac ports which is the official ODBC Driver for Microsoft SQL Server named msodbcsql17.

But at the time of writing an arm version for M1 macs was not available, so it doesn't work, and always threw an error.
It should work though on macs with an intel chip.

When installing msodbcsql17 it automatically creates a config in /opt/local/etc/odbcinst.ini, so one should be able to just use it in python using ODBC Driver 17 for SQL Server as the driver name:

import pyodbc
pyodbc.connect('DRIVER=ODBC Driver 17 for SQL Server;SERVER={IP address};PORT=1433;DATABASE={database};UID={username};PWD={password}')

When trying this on an M1 chip one will always get the error [unixODBC][Driver Manager]Can't open lib '/opt/local/lib/libmsodbcsql.17.dylib' : file not found (0) (SQLDriverConnect) even though that file exists.
This is because the file is in the architecture x86_64 as seen with lipo -info /opt/local/lib/libmsodbcsql.17.dylib. Before knowing this I tried forcing pyodbc to use it by installing it with the following command. LDFLAGS="-L/opt/local/lib -liodbc -liodbcinst" CFLAGS="-I/opt/local/include" pip install pyodbc This just resulted in the literal crash of the python programm with no error message from python.

@jtlz2
Copy link

jtlz2 commented Sep 12, 2022

@mariusdkm I get the "literal crash" on an M1 even though lipo reports arm64 architecture and forcing the flags. Any ideas?

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