ずっとPython環境はpyenv + Anacondaで構築していたのだが、Chat GPT APIなどcondaリポジトリに無いライブラリを取得するためにcondaとpipを併用するのが面倒くさくなったのでpoetryで環境を構築することにした。対応内容は以下の通り
1. pyenvでpythonをインストール
pyenv install 3.11.3
2. poetryをインストール
curl -sSL https://install.python-poetry.org | python
3. poetryの設定変更
プロジェクト配下に仮想環境を作成するため下記コマンドを実施
poetry config virtualenvs.in-project true
初期状態では仮想環境構築時にエラーとなるので下記コマンドを実施
poetry config virtualenvs.prompt null
4. 仮想環境構築
cd ~ poetry new base-py311
6. ログイン時に仮想環境を起動するようにzshrcに下記を追加
pyenv global 3.11.3 source /Users/hoge/base-py311/.venv/bin/activate
7.powerline-shell導入
※powerline-statusを導入していたが、仮想環境の表示がうまく行かなかったので変更した
インストール
pip install --user powerline-shell
.zshrcにパス追加
export PATH="/Users/hoge/.local/bin:$PATH"
.zshrcに関数追加
function powerline_precmd() { PS1="$(powerline-shell --shell zsh $?)" } function install_powerline_precmd() { for s in "${precmd_functions[@]}"; do if [ "$s" = "powerline_precmd" ]; then return fi done precmd_functions+=(powerline_precmd) } if [ "$TERM" != "linux" -a -x "$(command -v powerline-shell)" ]; then install_powerline_precmd fi
表示内容を変更するためにconfigをコピー
mkdir -p ~/.config/powerline-shell && \ powerline-shell --generate-config > ~/.config/powerline-shell/config.json