pyodbc: Cannot import `pyodbc` with latest version on Apple Silicon
Environment
To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit:
- Python:
3.10.6 - pyodbc:
4.0.35 - OS: macOS Ventura (13.0)
- DB: N/A
- driver: N/A
Issue
overview:
I upgraded my pyodbc version to the latest on PyPi (v4.0.35) and noticed I was getting a new error that I had never dealt with before. Specifically, when importing pyodbc I receive the following:
(0x0002): symbol not found in flat namespace '_SQLAllocHandle'
An issue for this was raised and closed a while ago; it references this exact problem: #885
expected behavior:
pyodbc should import without issue
steps to reproduce:
- Create new virtual environment and activate:
python -m venv .venv
source .venv/bin/activate
- Install latest
pyodbc:
pip install pyodbc
- Import
pyodbcand observe error:
python
>>> import pyodbc
>>> (0x0002): symbol not found in flat namespace '_SQLAllocHandle'
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 36
- Comments: 89 (12 by maintainers)
Hi @nleroy917! I’ve just had the same issue the other day. I managed to fix it with a
--no-binaryoption which forces pip to compile the module from source instead of installing from precompiled wheel.pip install --no-binary :all: pyodbcI would also like to upvote the issue because ver. 4.0.34 works fine without compiling from source. E. g. you may force pip to install specific version of pyodbc with:
pip install pyodbc==4.0.34Ok this works for me, Mac M2, Ventura.
brew install unixodbcpip uninstall pyodbcpip install --no-binary :all: pyodbcI’m running on Apple Silicon as well and I’m unable to install pyodbc. I don’t have a full development environment - not sure if that’s needed to install from sources and compile. I have tried
pip3 install pyodbc==4.0.34and I get:I came here with the same problem but we’re using
poetryrather thanpip. So in case anyone else needs it, thepoetryworkaround is to run:(or add the
--localflag to do it for a specific project).This configures poetry itself so won’t stop other machines on the same project doing binary installs.
FYI, pyodbc 4.0.34 didn’t include wheel files for ARM64 MacOSX, only regular macs.
https://pypi.org/project/pyodbc/4.0.34/#files https://pypi.org/project/pyodbc/4.0.35/#files
Yes, on M1 macs, please use the
--no-binaryworkaround for now.@anibal2j , a couple of things. Firstly, I’d definitely recommend using virtual environments when “pip installing”, but I would also suggest not using Python either from homebrew or the built-in version on your Mac. You may want to consider using a utility like
pyenv(see here) to install Python from scratch. That way, you will get a clean install of Python, effectively straight from python.org.PYENV SETUP (optional)
If you want to install Python with
pyenv, you can follow the instructions on the website, but to be honest they are a bit confusing so here is my summary:In your ~/.bash_profile file, add the following:
Then, in a NEW shell, you can install multiple versions of Python like so:
(they get installed to directory ~/.pyenv/versions)
To set the Python versions you would like available at the command line (in order of priority), use “pyenv global”:
After running the above command, the commands “python3.10”, “python3”, and “python” all point to the Python 3.10.7 installation, and “python3.8” points to Python 3.8.12 (you can’t choose the incremental version).
VIRTUAL ENVIRONMENTS
Even if you don’t want to use
pyenvI would definitely recommend installingpyodbcinto a virtual environment, for example:Try that and see if it works for you.
Still not working for me:
@anibal2j do you have unixodbc installed (brew install unixodbc)? If yes, try ``` export LDFLAGS=“-L/opt/homebrew/Cellar/unixodbc/your-version/lib” export CPPFLAGS=“-I/opt/homebrew/Cellar/unixodbc/your-version/include” pip install --no-binary :all: pyodbc
It doesn’t appear that worked 😕
Steps:
New
venv:Find version of
unixodbc:Export flags:
Install, verify version:
Test:
This was a very long road. I hope we’re at the end.
Version 5.1.0 has been released with both Intel and ARM binaries, so hopefully pip will do the right thing.
We’d appreciate any feedback from Mac users. I’ll keep this open for a while until we find out how well they will work.
THX !!!
@Emmanuel-Tsavaris thanks a lot,
pip install --no-binary :all: pyodbcworked fine for my MacOS M1 Max, python3.9.6. previourly I got this error:ImportError: dlopen(/opt/homebrew/lib/python3.10/site-packages/pyodbc.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_SQLAllocHandle'This is what worked for me:
@anibal2j On an Intel x64 Mac, you should be able to “pip install” pyodbc in the usual manner (from the available wheel on PyPi), so instead of:
python -m pip install --force-reinstall --no-binary :all: pyodbcYou should be able to just use:python -m pip install pyodbcHi I tried this method but it’s not working.
Python: 3.10.5 pyodbc: 4.0.35 OS: macOS Monterey 12.2 chip apple- M1
>>> import pyodbc Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dlopen({venv_path}env/lib/python3.10/site-packages/pyodbc.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_SQLAllocHandle'does any one have any other idea?
@nleroy917 This is a genuine issue so let’s keep this open for the time being. Other people will then be able to see it. Thank you pointing out this issue and also for your comprehensive notes (these are always greatly appreciated!). We will investigate and figure out what’s going on. Meanwhile, I will update the Wiki with the workaround.
This comment here https://github.com/microsoft/homebrew-mssql-release/issues/53#issuecomment-922208476 helped me a lot.
I have written up some instructions to get
pyodbcto work on the M1 with Microsoft ODBC drivers as well:One can only use
pyodbcwhen using Rosetta (x86 emulation). Follow the steps below.Uninstall M1 versions of brew packages (if installed at all):
Install x86 Homebrew alongside the ARM M1 homebrew:
Then use x86 homebrew like
arch -x86_64 /usr/local/bin/brew installor use the following alias (add to~/.bash_profile)Install the ODBC packages.
Create an x86
condaenv with:or if using
micromamba:Test with
on Mac M3 ok too. Thanks a lot.
Tested on a Macbook M2 (
pip install --upgrade pyodbc) with similar results as jordantshaw. Nice work, thank you!Macbook Pro M1.
I am able to successfully install and import pyodbc using conda by running.
conda install pyodbcI am able able to install using pip, but fails upon import.
pip install pyodbcIf I run
pip install --no-binary :all: pyodbcthen I am able to successfully install and import.Any ideas why it works fine with conda but not pip?
@falkben OK, thanks. I’m going to have to borrow my wife’s M1 mac and work on this.
i’ve tried in a conda environment (with unixodbc installed into the conda environment) and a
virtualenvvirtual environment with this new wheel and neither work.When installing with:
pip install --no-binary :all: --no-cache-dir --force-reinstall pyodbcit is able to import pyodbc without error
Ahh. Is this specifiable in a
requirements.txtfile?Just tested on Macbook M1, works as intended! Thank you for all the hard work that went into this. So glad its fixed!
I just tried this on my Apple M2 Pro, Ventura, python 3.9.16 (installed using asdf-vm):
edit: installing using
pip install --no-binary :all: pyodbc==5.0.0b2seems to work just fine.I have added Apple Silicon wheels to 5.0.0b2. Can someone please try installing them with just
pip install pyodbc==5.0.0b2? Thanks.In the past Github actions, which we use to build the wheels, did not support macOS ARM very well. Github is not finished, but it looks like there might be enough support now.
Having issues with
--no-binary :all: pyodbcI get errors to do with setup.py and so I tried using--use-pep517but still getsymbol not found in flat namespace '_SQLAllocHandle'. Please help, this issue is destroying meChange ENCRYPT=yes to ENCRYPT=no and see what happens. Not sure if in your environment this is OK or not, but depending on your situation it might be OK.
Excellent! that did it. All I have to do now is fix credentials - I’m reaching out to the vendor who installed the MS SQL server for credentials and I should be good to go. Thanks a lot for your help!
@anibal2j To talk to SQL Server with Python, you will need:
Your code is using the SQL Server driver called “ODBC Driver 18 for SQL Server”, but it looks like that driver is not yet installed. Check this by looking in your odbcinst.ini file (probably at location /usr/local/etc/odbcinst.ini but check with
odbcinst -j). There must be a section that starts with “[ODBC Driver 18 for SQL Server]”. If that doesn’t exist, install the driver using the instructions here, i.e.:(the first line in the Microsoft instructions simply installs homebrew)
After installation, check your odbcinst.ini file again. If that is OK now, try running your python code once more.
Do you have unixODBC (arm64 binary) installed?
Thank you for this. I came with the same issue and this fixed it.