Python – Setting python shell version with .sh file

bashpython3

I am executing my ./shell.sh 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.

(source)

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 shell.sh and you run it with ./shell.sh (or bash shell.sh) 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:

. ./shell.sh

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

Related Question