LoginSignup
0
0

More than 3 years have passed since last update.

pyenvが有効にならない場合の対処法(zsh)

Posted at

前提

  • macOS 10.15.7(Catalina)
  • zsh
  • pyenvはインストール済み
  • ここを参考に.zshenvに環境変数を記述

pyenvが使用されていないことの発見

pyenvで3.8.6に指定されていることを確認。

% pyenv versions
  system
* 3.8.6 (set by /Users/user/.pyenv/version)

ところが実際pythonのバージョンを確認したところ、3.8.6になってない!!

% python3 -V
Python 3.9.0
% python -V
Python 2.7.16

そもそもPATHを確認すると

% echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/user/.pyenv/bin

最初に /user/local/bin が来ている!

% which python
/usr/bin/python
% which python3
/usr/local/bin/python3

なるほど、pyenvが使われていないわけだ。
~/.zshenvはこうなっている

% cat ~/.zshenv
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

内容的には問題なさそう。
PATHの順番を書き換えるとなると、/etc/zprofileあたりか。

% cat /etc/zprofile
# System-wide profile for interactive zsh(1) login shells.

# Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
# and zshoptions(1) for more details.

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

こんなところ書き換える必要があるのか疑わしいので、pyenvの公式サイトを確認。
すると、~/.zshrc

~/.zshrc
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

とするよう書いてある、早速~/.zshenvに記載していた部分を~/.zshrcに移動して、ターミナル再起動!

すると、

% echo $PATH
/Users/user/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/user/.pyenv/bin
% python -V
Python 3.8.6
% which python
/Users/ham/.pyenv/shims/python

結論

  • zshの場合は環境変数は全て~/.zshenvに記載すればよいというわけではなく、~/.zshrcも使用する。
  • 公式ドキュメントを確認する。
0
0
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
0
0