pyenvをシェルスクリプトから使いたい時がある、
pyenvを有効化するためにはsource ~/.bashrc
を実行する必要があるので、スクリプトは以下のようになることが多い。
source ~/.bashrc
pyenv activate myenv
python -V
pyenv deactivate
pyenv activate myenv3.6.9
python -V
pyenv deactivate
pyenv activate myenv3.7.7
python -V
pyenv deactivate
しかし、これをそのままbash test.sh
のように実行するとエラーになる
$ bash test.sh
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Python 3.9.1
Failed to deactivate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Python 3.9.1
Failed to deactivate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Python 3.9.1
Failed to deactivate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
これはsource
がシェルスクリプトの中で有効になっていないためのようで、
source test.sh
で実行するとOK
$ source test.sh
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
Python 3.9.1
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
Python 3.6.9
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
Python 3.7.7