在 Mac 上安装 `pymssql` 时出错 -- FreeTDS

Error during install of `pymssql` on Mac -- FreeTDS

我已经回顾了关于这个主题的许多问题,但所有问题似乎都已过时或没有帮助。我正在尝试使用 pip3 设置 pymssql。我有 python 版本 3.7 和 freetds 版本 1.1.26。当我 运行 pip3 install pymssql 这里是我遇到的冗长错误。乍一看,我认为这可能是 freetds 版本 (see here) 的问题,我也尝试直接从 github 存储库安装:pip3 install git+https://github.com/pymssql/pymssql 但 wheel 无法构建。

Collecting pymssql
  Using cached pymssql-2.1.4.tar.gz (691 kB)
Building wheels for collected packages: pymssql
  Building wheel for pymssql (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/setup.py'"'"'; __file__='"'"'/private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-wheel-tfqq1pua
       cwd: /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/
  Complete output (35 lines):
  setup.py: platform.system() => 'Darwin'
  setup.py: platform.architecture() => ('64bit', '')
  setup.py: platform.libc_ver() => ('', '')
  setup.py: Detected Darwin/Mac OS X.
      You can install FreeTDS with Homebrew or MacPorts, or by downloading
      and compiling it yourself.

      Homebrew (http://brew.sh/)
      --------------------------
      brew install freetds

      MacPorts (http://www.macports.org/)
      -----------------------------------
      sudo port install freetds

  setup.py: Not using bundled FreeTDS
  setup.py: include_dirs = ['/usr/local/include']
  setup.py: library_dirs = ['/usr/local/lib']
  running bdist_wheel
  running build
  running build_ext
  cythoning src/_mssql.pyx to src/_mssql.c
  /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/src/_mssql.pxd
    tree = Parsing.p_module(s, pxd, full_module_name)
  warning: src/_mssql.pyx:150:4: Exception already a builtin Cython type
  cythoning src/pymssql.pyx to src/pymssql.c
  /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/src/pymssql.pyx
    tree = Parsing.p_module(s, pxd, full_module_name)
  building '_mssql' extension
  creating build
  creating build/temp.macosx-10.9-x86_64-3.7
  creating build/temp.macosx-10.9-x86_64-3.7/src
  gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/usr/local/include -I/Library/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c src/_mssql.c -o build/temp.macosx-10.9-x86_64-3.7/src/_mssql.o -DMSDBLIB
  xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for pymssql
  Running setup.py clean for pymssql
Failed to build pymssql
Installing collected packages: pymssql
    Running setup.py install for pymssql ... error
    ERROR: Command errored out with exit status 1:
     command: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/setup.py'"'"'; __file__='"'"'/private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-record-tjfo0sbt/install-record.txt --single-version-externally-managed --compile --install-headers /Library/Frameworks/Python.framework/Versions/3.7/include/python3.7m/pymssql
         cwd: /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/
    Complete output (35 lines):
    setup.py: platform.system() => 'Darwin'
    setup.py: platform.architecture() => ('64bit', '')
    setup.py: platform.libc_ver() => ('', '')
    setup.py: Detected Darwin/Mac OS X.
        You can install FreeTDS with Homebrew or MacPorts, or by downloading
        and compiling it yourself.

        Homebrew (http://brew.sh/)
        --------------------------
        brew install freetds

        MacPorts (http://www.macports.org/)
        -----------------------------------
        sudo port install freetds

    setup.py: Not using bundled FreeTDS
    setup.py: include_dirs = ['/usr/local/include']
    setup.py: library_dirs = ['/usr/local/lib']
    running install
    running build
    running build_ext
    cythoning src/_mssql.pyx to src/_mssql.c
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/src/_mssql.pxd
      tree = Parsing.p_module(s, pxd, full_module_name)
    warning: src/_mssql.pyx:150:4: Exception already a builtin Cython type
    cythoning src/pymssql.pyx to src/pymssql.c
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/src/pymssql.pyx
      tree = Parsing.p_module(s, pxd, full_module_name)
    building '_mssql' extension
    creating build
    creating build/temp.macosx-10.9-x86_64-3.7
    creating build/temp.macosx-10.9-x86_64-3.7/src
    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/usr/local/include -I/Library/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c src/_mssql.c -o build/temp.macosx-10.9-x86_64-3.7/src/_mssql.o -DMSDBLIB
    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/setup.py'"'"'; __file__='"'"'/private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-install-2jerl6n6/pymssql/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/ty/b0h4wy9938vbwh98tn_r9ryh0000gn/T/pip-record-tjfo0sbt/install-record.txt --single-version-externally-managed --compile --install-headers /Library/Frameworks/Python.framework/Versions/3.7/include/python3.7m/pymssql Check the logs for full command output.

更新:看来 pymssql 项目可能再次激活。原来的停产公告在这里:https://github.com/pymssql/pymssql/issues/668


很遗憾,pymssql 项目已停止: https://github.com/pymssql/pymssql

如果您需要连接到 SQL 服务器,我建议您使用 pyodbc。这是快速版本,看起来您已经完成了 FreeTDS 安装。

brew install freetds
python3 -m venv pyodbc_venv
source pyodbc_venv/bin/activate
pip install pyodbc

然后,在你的连接中明确,因为这解决了大多数问题:

conn = pyodbc.connect('DRIVER={FreeTDS};SERVER=yourserver.yourcompany.com;PORT=1433;DATABASE=yourdb;UID=user;PWD=password;TDS_Version=7.3')

pyodbc 的完整文档在此处:https://github.com/mkleehammer/pyodbc/wiki

祝你好运!

在 MacOs Big Sur 上

brew install freetds openssl
export LDFLAGS="-L/opt/homebrew/opt/freetds/lib -L/opt/homebrew/opt/openssl@1.1/lib"
export CFLAGS="-I/opt/homebrew/opt/freetds/include"
pip3 install pymssql