Python複数バージョン

Windows

  • インストーラーで各バージョンをインストール
  • パワーシェルでpy --listで可能なバージョンが表示
  • 使用する場合は例えばpy -3.10 -m venv .venv

Mac

pyenv

https://github.com/pyenv/pyenv

  • インストール
    brew install pyenv
  • PATHを通す
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init - zsh)"' >> ~/.zshrc
  • シェルを再起動

    exec "$SHELL"
    • 推奨ビルド環境

    • pyenvがダウンロードしたPythonのビルドが環境の依存性で失敗する、またはランタイムで挙動がおかしいことがあるらしい。

    • そこで、次のインストールが推奨されているので、実行する。

      brew install openssl readline sqlite3 xz zlib tcl-tk@8 libb2

      すると以下のメッセージが出るが、とりあえず実行しないでおく

      sqlite is keg-only, which means it was not symlinked into /opt/homebrew,
      because macOS already provides this software and installing another version in
      parallel can cause all kinds of trouble.

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

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

      For pkg-config to find sqlite you may need to set:
      export PKG_CONFIG_PATH="/opt/homebrew/opt/sqlite/lib/pkgconfig"
      ...
      zlib is keg-only, which means it was not symlinked into /opt/homebrew,
      because macOS already provides this software and installing another version in
      parallel can cause all kinds of trouble.

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

      For pkg-config to find zlib you may need to set:
      export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"

      ...
      The sqlite3_analyzer binary is in the sqlite-analyzer formula.

      tcl-tk@8 is keg-only, which means it was not symlinked into /opt/homebrew,
      because this is an alternate version of another formula.

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

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

      For pkg-config to find tcl-tk@8 you may need to set:
      export PKG_CONFIG_PATH="/opt/homebrew/opt/tcl-tk@8/lib/pkgconfig"

    • 特定のバージョンをインストール

      pyenv install 3.11
    • Pythonバージョンの変更

      pyenv shell -- select just for current shell session
      pyenv local -- automatically select whenever you are in the current directory (or its subdirectories)
      pyenv global -- select globally for your user account