B-Teck!

お仕事からゲームまで幅広く

【Python】pyenv + Poetry + IntelliJでPythonの開発環境を構築する

Mac向けです。
brewはインストール済みの前提で進めます。

pyenvインストール

macOS Catalina zsh環境でpyenvを使ってPython 3.9.0をインストール
コマンド - Poetry documentation (日本語訳)

$ brew install pyenv

.zshrcに追記

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

インストール可能なバージョン一覧を確認、インストール

$ pyenv install --list
$ pyenv install 3.9.4
$ pyenv global 3.9.4

Poetryインストール

M1 Macにpyenv+Poetryをインストールする | mktia's note

$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

作成済みのリポジトリの場合

$ cd ${path} 
$ poetry init

新しくリポジトリを作る場合

$ poetry new ${path}

IntelliJでPoetryを使う

  1. Poetryのプラグイン をインストールする
  2. File > Project Structureを開く
    1. Project Settings -> Project を開く
    2. Project SDK -> Add SDK -> Python SDKを開く
    3. Poetry Environment -> Existing Environment Interpreterから利用したいvenvを指定
  3. Run > Edit Configrations > +(Add New Configrations)
    1. Pythonを選択
    2. Configration > Script Pathでアプリケーションのエントリポイントを指定