For those who use python in the pyenv
+ pyenv-virtualenv
environment.
I want to automatically load .python-version
and ʻactivate` when I move the directory.
Reason,
cd ~/foo
pyenv activate foo
cd ~
pyenv deactivate
Because it is troublesome to activate and deactivate one by one.
If you are using direnv
, use ʻeval" $ (direnv hook bash) " If it is described as follows in
~ / .bashrc etc., every time you
cd It seems that it hooks me, so I should be able to do it without using
direnv. It was easy to do after a little research, so I posted it after a long time as a reminder (Maybe I should use
direnv`, but I personally didn't get used to it)
correction:
eval "$(pyenv virtualenv-init -)"
And, if it is described in ~ / .bash_profile
, it will switch automatically ...
I intended to have described it above, but there was a case where the environment did not switch because the description position was wrong ...
It is assumed that the pyenv
and pyenv-virtualenv
environments have been set.
Then add the following to .bash_profile
etc. to .bash_profile
etc.
cd(){
now_path=`pwd`
now_pyenv_filepath=$now_path/.python-version
if [ -e ${now_pyenv_filepath} ];then
now_pyenv=`cat ${now_pyenv_filepath}`
pyenv deactivate
fi
new_pyenv_filepath=$1/.python-version
if [ -e ${new_pyenv_filepath} ];then
local_env=`cat ${new_pyenv_filepath}`
pyenv activate ${local_env}
fi
builtin cd "$1"
}
If you write the above and reload it as source .bash_profile
It will automatically deactivate
and ʻactivate`.
First read the current location with pwd
If there is .python-version
there, the first half is deactivate
Furthermore, if there is .python-version
in the destination folder
Load it ʻactivate and finally load the built-in
cd`
I'm moving.
I couldn't call the built-in command with bulitin hoge
.
I referred to the following that I went through with shell hook
.
Recommended Posts