Requirements

  • brew: macOS package manager
  • pyenv: python environment manager
  • poetry: python project package manager

Environment Setup

  1. Install dependencies
brew install unixodbc
brew install freetds
brew install openssl
brew install libiconv

TIP

如果沒有使用poetry的話,可以只執行下面這段指令

pip install --pre --no-binary :all: pymssql --no-cache --force
  1. Install python with pyenv
  2. Change global python to previously installed one
# choice any version you prefer
PY_VERSION='3.10'
pyenv install $PY_VERSION
pyenv global $PY_VERSION
  1. Create virtual env with poetry
poetry env use $PY_VERSION
  1. Setup Compile Flags
export LDFLAGS="-L/opt/homebrew/opt/freetds/lib -L/opt/homebrew/opt/openssl@3/lib"
export CFLAGS="-I/opt/homebrew/opt/freetds/include"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

https://github.com/pymssql/pymssql/issues/727#issuecomment-1204850473

  1. Install pymssql with poetry
poetry add pymssql