LoginSignup
2
2

More than 5 years have passed since last update.

preztoのテーマ「pure」でpipenv shell使用時の環境名が「.venv」になる現象への対応メモ

Last updated at Posted at 2019-01-12

現象

pipenvでPIPENV_VENV_IN_PROJECTを設定するとプロジェクトディレクトリの下に仮想環境が作成されるようになる。
参考: https://dackdive.hateblo.jp/entry/2018/05/16/100000

zshのテーマにpreztoのpureを使っていると、仮想環境を切り替えた際に環境名が「.venv」と表示されてしまう。

~/Documents
❯ mkdir test && cd test

~/Documents/test
❯ pipenv --three
Creating a virtualenv for this project…

~/Documents/test
❯ l
.venv  # プロジェクトディレクトリ下に仮想環境が作成される
Pipfile

~/Documents/test
❯ pipenv shell  # 仮想環境の切り替え
…

~/Documents/test
.venv ❯  # 環境名が「.venv」になる!

原因

pureのスクリプト内で、仮想環境の設定の場所を示す環境変数VIRTUAL_ENVのbasenameを環境名として使用しているため。

~/Documents/test
.venv ❯ echo ${VIRTUAL_ENV}
/Users/(User)/Documents/test/.venv

~/Documents/test
.venv ❯ echo ${VIRTUAL_ENV:t}  # zshでは:tをつけるとbasenameになる
.venv

対応

pureのスクリプト~/.zprezto/modules/prompt/functions/prompt_pure_setupを修正する。
${VIRTUAL_ENV:t}.venvの場合に、その一つ上のディレクトリ名を環境名として設定する。

~
❯ cd .zprezto/modules/prompt/functions/  # ここに各テーマのスクリプトがある

~/.zprezto/modules/prompt/functions master* ⇣
❯ cp prompt_pure_setup prompt_pure_setup.org  # pureのスクリプトのバックアップ作成

~/.zprezto/modules/prompt/functions master* ⇣
❯ vim prompt_pure_setup  # 修正

~/.zprezto/modules/prompt/functions master* ⇣
❯ diff prompt_pure_setup.org prompt_pure_setup  # 修正箇所は以下の通り
192c192,197
<       psvar[12]="${VIRTUAL_ENV:t}"
---
>       env_name="${VIRTUAL_ENV:t}"
>       if [[ "x${env_name}" = "x.venv" ]]; then
>           psvar[12]="${VIRTUAL_ENV:h:t}"
>       else
>           psvar[12]="${env_name}"
>       fi

修正後の確認

~/Documents/test
❯ pipenv shell  # 仮想環境の切り替え
…

~/Documents/test
test ❯  # 環境名がプロジェクトディレクトリ名になった
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2