5

I am trying to install pyodbc with pip on macOS(12.3.1), but that didn't work.

In error log, Message says "fatal error: 'sql.h' file not found". Some people are helped by the command brew install unixodbc. I did run brew install unixodbc, but errors remain emerge.

My environment: Macbook pro M1 pip (22.0.4) Python (3.10) pyenv (2.2.5)

I'm sorry for broken english and poor understand on python.

% pip3 install pyodbc                              

Collecting pyodbc
  Using cached pyodbc-4.0.32.tar.gz (280 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: pyodbc
  Building wheel for pyodbc (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [14 lines of output]
      running bdist_wheel
      running build
      running build_ext
      building 'pyodbc' extension
      creating build
      creating build/temp.macosx-12.1-arm64-3.10
      creating build/temp.macosx-12.1-arm64-3.10/src
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -DPYODBC_VERSION=4.0.32 -UMAC_OS_X_VERSION_10_7 -I/usr/local/include -I/Users/daiki/.pyenv/versions/3.10.0/include/python3.10 -c src/buffer.cpp -o build/temp.macosx-12.1-arm64-3.10/src/buffer.o -Wno-write-strings -Wno-deprecated-declarations
      In file included from src/buffer.cpp:12:
      src/pyodbc.h:56:10: fatal error: 'sql.h' file not found
      #include <sql.h>
               ^~~~~~~
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for pyodbc
  Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: pyodbc
  Running setup.py install for pyodbc ... error
  error: subprocess-exited-with-error
  
  × Running setup.py install for pyodbc did not run successfully.
  │ exit code: 1
  ╰─> [14 lines of output]
      running install
      running build
      running build_ext
      building 'pyodbc' extension
      creating build
      creating build/temp.macosx-12.1-arm64-3.10
      creating build/temp.macosx-12.1-arm64-3.10/src
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -DPYODBC_VERSION=4.0.32 -UMAC_OS_X_VERSION_10_7 -I/usr/local/include -I/Users/daiki/.pyenv/versions/3.10.0/include/python3.10 -c src/buffer.cpp -o build/temp.macosx-12.1-arm64-3.10/src/buffer.o -Wno-write-strings -Wno-deprecated-declarations
      In file included from src/buffer.cpp:12:
      src/pyodbc.h:56:10: fatal error: 'sql.h' file not found
      #include <sql.h>
               ^~~~~~~
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> pyodbc

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.
Suzushio
  • 61
  • 1
  • 2
  • 1
    Does this answer your question? [Installing pyodbc fails on OSX 12.2 (Monterey)](https://stackoverflow.com/questions/71138425/installing-pyodbc-fails-on-osx-12-2-monterey) – Gino Mempin May 06 '22 at 13:43

2 Answers2

17

TL, DR

brew install unixodbc
export LDFLAGS="-L/opt/homebrew/Cellar/unixodbc/[your version]/lib" 
export CPPFLAGS="-I/opt/homebrew/Cellar/unixodbc/[your version]/include"
pip install pyodbc

Explain

pyodbc doesn't have pre-build wheels for M1 mac. See https://pypi.org/project/pyodbc/#files . If I remember correctly, pre-build wheels for M1 should have tag looks like pandas-1.4.3-cp38-cp38-macosx_10_9_universal2.whl, "universal".

So what pip does is downloading the source (tar.gz) and trying to build locally. Then you need some C header files and source files to in order to bulid.

brew install unixodbc install those files. But for brew installed on M1 mac, those files are not installed in system default search directories. So that's why you need to manually speicify some additional directories for the compiler to search and then run pip install pyodbc:

export LDFLAGS="-L/opt/homebrew/Cellar/unixodbc/[your version]/lib" 
export CPPFLAGS="-I/opt/homebrew/Cellar/unixodbc/[your version]/include"

related issue: https://github.com/mkleehammer/pyodbc/issues/988

Rick
  • 7,007
  • 2
  • 49
  • 79
  • thank you for your answer, but I keep getting all the same errors as described in the initial question. 'unixodbc' already installed and up to date with the latest version: 2.3.11 Any other idea that I can try? *Note*, I believe at the end of the 3rd paragraph should be ''' pip install pyodbc ''' – user3891775 Sep 30 '22 at 19:32
  • @user3891775 still `'sql.h' file not found`? Check if you do have files installed under these 2 directories`/opt/homebrew/Cellar/unixodbc/[your version]/lib`, `/opt/homebrew/Cellar/unixodbc/[your version]/include` – Rick Oct 01 '22 at 11:22
  • thanks Rick for your input. I was using the default python3, that comes pre-installed in Mac(Macbook pro M1 chip). After spending several hours, I ditched the pre-installed and installed python3 using homebrew; after that I was able to install all packages to success. For your comment related to 'sql.h' file; I only found it in "/opt/homebrew/Cellar/unixodbc/[your version]/include" – user3891775 Oct 03 '22 at 12:42
  • This is a great, updated example to another solution that had me export those variables. Thank you! – Zach Fleeman Jun 14 '23 at 22:52
2

I encountered the problem when trying to install pyodbc==4.0.31 but performing an update to pyodbc==4.0.34 solved my problem.

Degim
  • 51
  • 2