【问题标题】:Unable to pip install pyodbc on mac无法在 mac 上 pip 安装 pyodbc
【发布时间】:2022-07-12 16:27:24
【问题描述】:

我正在尝试在 macOS(12.3.1) 上使用 pip 安装 pyodbc,但这不起作用。

在错误日志中,消息说“致命错误:找不到'sql.h'文件”。命令brew install unixodbc 帮助了一些人。我确实运行了brew install unixodbc,但仍然出现错误。

我的环境: MacBook Pro M1 点(22.0.4) 蟒蛇 (3.10) pyenv (2.2.5)

对不起,英语不好,对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.

【问题讨论】:

标签: python pip pyodbc


【解决方案1】:

pyodbc 没有用于 M1 mac 的预构建*。见https://pypi.org/project/pyodbc/#files。如果我没记错的话,M1 的预制*应该有类似pandas-1.4.3-cp38-cp38-macosx_10_9_universal2.whl、“universal”的标签。

所以 pip 所做的是下载源代码 (tar.gz) 并尝试在本地构建。然后你需要一些C头文件和源文件来构建。

brew install unixodbc 安装这些文件。但是对于安装在 M1 mac 上的brew,这些文件不会安装在系统默认搜索目录中。所以这就是为什么你需要先手动为编译器指定一些目录,然后运行pip install unixodbc

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

相关问题:https://github.com/mkleehammer/pyodbc/issues/988

【讨论】: