Python – Setting python shell version with .sh file


I am executing my ./ which contains

eval "$(pyenv init -)"
pyenv shell 3.8.2

But nothing happens. If i run this code in bash manually it works fine and shell version of python switches from 2.7.10 to 3.8.2.

I am in the right directory and I gave the .sh file execution permissions.

Best Answer

Add pyenv init to your shell to enable shims and autocompletion. Please make sure eval "$(pyenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH during the initialization.


The command is designed to manipulate the environment of the shell; and to define some functions. If it's in a script then it will manipulate the environment of (and define functions in) the shell interpreting the script.

If your script is and you run it with ./ (or bash then the shell interpreting the script is not your current shell. Please see What is the difference between sourcing (. or source) and executing a file in bash?

So don't expect your script to change anything in your current shell, unless you source the script:

. ./

Note in this case the file does not need to be executable. The shebang (if any) doesn't matter.

Related Question