背景:m2在终端下想要访问SQLserver数据库,就需要在Python中安装pymssql的模块,我用的是pip命令安装的,命令pip install ,具体报错信息如下;

pip install pymssql                                                                 ✔  took 7s  base Py  at 11:38:10 
Collecting pymssql
  Downloading pymssql-2.2.7.tar.gz (170 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 171.0/171.0 kB 330.8 kB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pymssql
  Building wheel for pymssql (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for pymssql (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [25 lines of output]
      setup.py: platform.system() => Darwin
      setup.py: platform.architecture() => ('64bit', '')
      setup.py: platform.libc_ver() => ('', '')
      setup.py: include_dirs => []
      setup.py: library_dirs => []
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-11.1-arm64-cpython-39
      creating build/lib.macosx-11.1-arm64-cpython-39/pymssql
      copying src/pymssql/__init__.py -> build/lib.macosx-11.1-arm64-cpython-39/pymssql
      running build_ext
      cythoning src/pymssql/_mssql.pyx to src/pymssql/_mssql.c
      cythoning src/pymssql/_pymssql.pyx to src/pymssql/_pymssql.c
      building 'pymssql._mssql' extension
      creating build/temp.macosx-11.1-arm64-cpython-39
      creating build/temp.macosx-11.1-arm64-cpython-39/src
      creating build/temp.macosx-11.1-arm64-cpython-39/src/pymssql
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/wuzhanxi/opt/anaconda3/include -arch arm64 -I/Users/wuzhanxi/opt/anaconda3/include -fPIC -O2 -isystem /Users/wuzhanxi/opt/anaconda3/include -arch arm64 -I/Users/wuzhanxi/opt/anaconda3/include/python3.9 -c src/pymssql/_mssql.c -o build/temp.macosx-11.1-arm64-cpython-39/src/pymssql/_mssql.o -DMSDBLIB
      src/pymssql/_mssql.c:747:10: fatal error: 'sqlfront.h' file not found
      #include "sqlfront.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 pymssql
Failed to build pymssql
ERROR: Could not build wheels for pymssql, which is required to install pyproject.toml-based projects
解决方案:
第一步:用brew安装freetds openssl
brew install freetds openssl

执行完显示如下;

brew install freetds openssl                                                                1 ✘  base Py  at 12:22:39 
Running `brew update --auto-update`...
Error: Failed to download https://formulae.brew.sh/api/formula.jws.json!
Failed to download https://formulae.brew.sh/api/cask.jws.json!
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/api/formula.jws.json
curl: (28) Failed to connect to mirrors.ustc.edu.cn port 443 after 75014 ms: Operation timed out

==> Downloading https://formulae.brew.sh/api/formula.jws.json
curl: (28) Failed to connect to formulae.brew.sh port 443 after 262505 ms: Operation timed out

Warning: formula.jws.json: update failed, falling back to cached version.
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/api/cask.jws.json
curl: (28) Failed to connect to mirrors.ustc.edu.cn port 443 after 75025 ms: Operation timed out

==> Downloading https://formulae.brew.sh/api/cask.jws.json
curl: (28) Failed to connect to formulae.brew.sh port 443 after 262513 ms: Operation timed out

Warning: cask.jws.json: update failed, falling back to cached version.
==> Fetching dependencies for freetds: m4, libtool, and unixodbc
==> Fetching m4
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/m4-1.4.19.arm64_ventura.bottle.tar.gz
######################################################################## 100.0%
==> Fetching libtool
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/libtool-2.4.7.arm64_ventura.bottle.tar.gz
######################################################################## 100.0%
==> Fetching unixodbc
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/unixodbc-2.3.11.arm64_ventura.bottle.tar.gz
######################################################################## 100.0%
==> Fetching freetds
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/freetds-1.3.18.arm64_ventura.bottle.tar.gz
######################################################################## 100.0%
==> Fetching openssl@3
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/openssl%403-3.1.0.arm64_ventura.bottle.tar.gz
######################################################################## 100.0%
==> Installing dependencies for freetds: m4, libtool, and unixodbc
==> Installing freetds dependency: m4
==> Pouring m4-1.4.19.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/m4/1.4.19: 13 files, 742.2KB
==> Installing freetds dependency: libtool
==> Pouring libtool-2.4.7.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/libtool/2.4.7: 75 files, 3.8MB
==> Installing freetds dependency: unixodbc
==> Pouring unixodbc-2.3.11.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/unixodbc/2.3.11: 48 files, 2.3MB
==> Installing freetds
==> Pouring freetds-1.3.18.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/freetds/1.3.18: 1,262 files, 14MB
==> Running `brew cleanup freetds`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Pouring openssl@3-3.1.0.arm64_ventura.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /opt/homebrew/etc/openssl@3/certs

and run
  /opt/homebrew/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

==> Summary
🍺  /opt/homebrew/Cellar/openssl@3/3.1.0: 6,494 files, 28.4MB
==> Running `brew cleanup openssl@3`...
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Removing: /Users/wuzhanxi/Library/Caches/Homebrew/redis--7.0.9.arm64_ventura.bottle.tar.gz... (1006.8KB)
Removing: /Users/wuzhanxi/Library/Logs/Homebrew/redis... (64B)
Removing: /Users/wuzhanxi/Library/Logs/Homebrew/ca-certificates... (64B)
Removing: /Users/wuzhanxi/Library/Logs/Homebrew/openssl@1.1... (64B)
Pruned 0 symbolic links and 6 directories from /opt/homebrew
==> Caveats
==> openssl@3
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /opt/homebrew/etc/openssl@3/certs

and run
  /opt/homebrew/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
第二步:根据上面的信息提示,将一些环境变量配置到~/.zshrc中

分别执行命令如下;

echo 'export LDFLAGS="-L/opt/homebrew/opt/freetds/lib -L/opt/homebrew/opt/openssl@3/lib"' >> ~/.zshrc  

echo 'export CFLAGS="-I/opt/homebrew/opt/freetds/include"' >> ~/.zshrc

echo 'export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"' >> ~/.zshrc

第三步:让配置文件立即生效
 source ~/.zshrc
第四步:安装pymssql
pip install pymssql

执行完显示如下;

Collecting pymssql
  Using cached pymssql-2.2.7.tar.gz (170 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pymssql
  Building wheel for pymssql (pyproject.toml) ... done
  Created wheel for pymssql: filename=pymssql-2.2.7-cp39-cp39-macosx_11_0_arm64.whl size=251793 sha256=c96a522d744214a9c3b4d57d9500851e7c4a8dfdc2776bb26423c6e76faa14df
  Stored in directory: /Users/wuzhanxi/Library/Caches/pip/wheels/73/6e/49/95defb17087171edcc2306c7118331b3ce4a7d6c71d113496e
Successfully built pymssql
Installing collected packages: pymssql
Successfully installed pymssql-2.2.7
第五步:验证是否安装pymssql 成功了

在终端下输入:Python
然后输入import pymssql,如下图所示则表示安装成功了

 ~/m/s/code_environment/python_all_package  python                                                                              ✔  took 40s  base Py  at 13:37:20 
Python 3.9.13 (main, Aug 25 2022, 18:24:45) 
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymssql
>>> 

在开发过程中遇到的问题,文档写的比较急,有问题欢迎评论区留言

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐