Unable to import pyodbc on Apple Silicon - Symbol not found: _SQLAllocHandle
Asked Answered
W

2

20

I am currently working on a python (3.8) project on my 2021 MacBook Pro with Apple Silicon. Ultimately, the goal is to build a ML model on data I read from an Azure SQL DB using Apple's Tensorflow fork. Therefore, I am developing the project on native Apple Silicon packages - not using Rosetta.

The Problem arises when I try to import the pyodbc package (4.0.30) in order to connect to my DB. I keep getting the following error

  File "<stdin>", line 1, in <module>
ImportError: dlopen({myvenv}/lib/python3.8/site-packages/pyodbc.cpython-38-darwin.so, 2):
Symbol not found: _SQLAllocHandle
  Referenced from: {myvenv}/lib/python3.8/site-packages/pyodbc.cpython-38-darwin.so
  Expected in: flat namespace
 in {myvenv}/lib/python3.8/site-packages/pyodbc.cpython-38-darwin.so

If however, I do the exact same thing using Rosetta everything works fine. I couldn't find any other thread describing a similar behaviour.

Does anyone know how to resolve this issue?

Weightless answered 21/3, 2021 at 10:8 Comment(4)
When you installed pyodbc did pip use the pre-compiled wheel (.whl) file?Questor
pip built from source as there is no wheel available yet for apple silicon unfortunately. Is there anything to consider when building pyodbc from source?Weightless
Well, on Mac there's at least the whole iODBC/unixODBC thing so you may want to raise an issue on GitHub to see if any of the developers might have an idea.Questor
Did you ever manage to fix this?Anuran
P
52

My feeling is that the package is not compiled properly for ARM architecture.

You can uninstall the pyodbc and install it again. If using pip, it would be like this:

pip uninstall pyodbc

and install with compiling it locally:

pip install --no-binary :all: pyodbc

Do not forget on Unix/Linux like platforms, you need to download the unixodbc source distribution, as pyodbc is build against an ODBC driver manager, i.e. unixodbc is a prerequisite. Example installation with brew package manager:

brew install unixodbc
Pineda answered 20/1, 2023 at 23:53 Comment(2)
Installing unixodbc was inadequate. It is better to go through Microsoft's installation process here: learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/…Bobby
@Bobby pyodbc uses unixodbc, so it is adequate. My feeling is that Microsoft ODBC driver for SQL Server (macOS) 17 and 18 also use as a dependency unixodbc, so probably it will solve the needed library installation, but the core of the answer is still the same, to go with local compilation of pyodbc to get the proper cpu architecture in place for pyodbc setup.Pineda
P
4

The not-compiled installation should work:

pip install --pre --no-binary :all: pyodbc

This compiles locally and is thus compatible and overcomes the issue.

Pineda answered 7/2, 2023 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.