in any case of using Apple silicon chips you may encounter issues running ODBC drivers; I've managed to make it run on my M1 Max - Follow these steps (location paths may vary...):
Install Homebrew (if not already installed):
Open Terminal and execute the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install ODBC Driver Manager via Homebrew:
Run the following command in Terminal:
brew install unixodbc
Install Microsoft ODBC Driver for SQL Server via Homebrew:
Execute the following command in Terminal:
brew tap microsoft/mssql-release GitHub - microsoft/homebrew-mssql-release brew update brew install msodbcsql17 mssql-tools
Set environment variables:
Open your shell configuration file (e.g., ~/.bash_profile
, ~/.zshrc
, etc.) in a text editor.
Add the following lines to set the environment variables:
export PATH="$PATH:/usr/local/opt/mssql-tools/bin"
export LDFLAGS="-L/usr/local/opt/unixodbc/lib"
export CPPFLAGS="-I/usr/local/opt/unixodbc/include"
Save the file and close it.
Run source <path_to_file>
to apply the changes to your current shell session.
(the paths may be different for you, so verify that they are correct prior to this step).
- Reinstall 'pyodbc':
Activate your virtual environment (if using one).
Run:
pip install --no-binary :all: pyodbc
to reinstall the 'pyodbc' package.
After following these steps, 'pyodbc' should work correctly with the updated ODBC driver manager and SQL Server driver. You can then import and use pyodbc
in your Python code without encountering the symbol not found
error.
If you're encountering the error message Can't open lib 'ODBC Driver 17 for SQL_Server' : file not found (0)
on macOS with Apple Silicon, it could be due to the architecture mismatch between the ODBC driver and the Python interpreter.
Check the architecture of your Python interpreter:
Open Terminal and execute the following command:
python3 -c "import platform; print(platform.architecture())"
Make sure the output shows arm64
for the architecture. If it shows x86_64
, you might need to use a Python interpreter built for Apple Silicon.
Obtain and install the Apple Silicon-compatible ODBC driver:
Visit the Microsoft ODBC Driver for SQL Server download page: Download ODBC Driver for SQL Server - ODBC Driver for SQL Server
Select the appropriate driver version for macOS with Apple Silicon (ARM64).
Follow the installation instructions provided by Microsoft to install the driver.
Verify the driver installation:
Run the following command in Terminal:
odbcinst -j
Check the value of the ODBCINSTINI
environment variable listed in the output.
Ensure that the directory mentioned in the ODBCINSTINI
variable contains an entry for the installed ODBC driver.
- Retry connecting to the SQL Server using 'pyodbc':
Update your connection string in your Python code to use the correct driver name.
For example:
Using Pyodbc:
`conn_str = 'DRIVER=<path to ODBC Driver 17 for SQL Server>;SERVER=<server_name>;DATABASE=<database_name>;UID=<username>;PWD=<password>`
connection = pyodbc.connect(conn_str)
Or if your’e using Sqlalchemy:
`conn_str = mssql+pyodbc://<Username>:<Pass>@<Address>:<Port>/<DB_name>?&driver=<Path to ODBC Driver 17 for SQL Server>`
engine = create_engine(url=conn_str, module=pyodbc)
- make sure to edit both these files using text editor (your location may vary):
/etc/odbcinst.ini
/etc/odbc.ini
insert these lines:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.17.dylib
UsageCount=5
<<(this line is optional)
By following these steps, you should be able to resolve the issue and establish a successful connection to the SQL Server using pyodbc
on macOS with Apple Silicon.