Using alias for changing the default python version

Viewed 1896

I would like to know if using alias in terminal to change the default python version has positive effects on subsequent calls of python in scripts?

For example, I use

$ alias python=python36
$ ./run_program

Now assume run_program is a script that call multiple nested scripts and inside them, the command is python foo.py. Will they execute python36 or python2?

Or that alias affects only direct python commands in current shell?

2 Answers

alias are not exported to sub-shells. It is better to use functions for this purpose. If you want systemwide override of python2 to python36 then use this code in your $HOME/.bashrc:

python() { python36 "$@"; }

export -f python

Then check python version in any script by using:

python -V

DO NOT DO THIS IN YOUR SYSTEM

Just tried that out of curiosity. And it does work, not sure what broke if anything.

$ python --version
Python 2.7.12
$ sudo cp /usr/bin/python /usr/bin/python.bak
$ sudo cp /usr/bin/python3 /usr/bin/python
$ python --version
Python 3.7.4

And, since I have no interested in finding what broke:

$ sudo mv /usr/bin/python.bak /usr/bin/python
Related