I just created my first AWS EC2 instance. I used sudo yum install python3 -y to install Python3 but when I check the version via python --version it says Python 2.7.16. How do I switch versions? 
- 875
 - 2
 - 10
 - 27
 
- 
                    3Use the `python3` command – SuperStormer Apr 11 '20 at 23:54
 - 
                    That didn't seem to do anything. Afterwards, I checked the version again and it said 2.7. – IamWarmduscher Apr 12 '20 at 00:02
 - 
                    As in, use `python3` instead of `python` – SuperStormer Apr 12 '20 at 00:08
 - 
                    How do I remove Python2? sudo yum uninstall python didn't work. – IamWarmduscher Apr 12 '20 at 00:15
 - 
                    `$ alias python=python37` – Aditya Rajgor Apr 24 '22 at 05:35
 
2 Answers
You can either invoke python 3 with python3 directly from the terminal, or create an alias by adding the following line to your ~/.bashrc or ~/.bash_aliases file:
alias python=python3
More details and troubleshooting tips available in this related question.
- 1,807
 - 5
 - 16
 
There are a couple of ways to do this:
You could explicitly request
python3, invoking it as-is, instead of justpython, i.e.:$ python3 Python 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>As per
@Nick Walsh's answer, you can create a shellalias(1)that just expandspythontopython3by putting the following either into.profile,.bashrc, or even.bash_aliases:alias python=python3Granted
python3is in yourPATH, this will work without a hitch, with the added benefit that this is a per user setting, meaning you won't be changing the system-widepythoninterpreter (sincepythonremains pointing to/usr/bin/python2). If you'd like, you can opt for a system-wide alias as well by modifying/etc/profileor/etc/bashrc, adding thealiasthere.You could replace the
pythonsymlink, linking it topython3instead.You can achieve this using
ln(1)(pay close attention to the#vs.$prompt, meaning you require root privileges to issue this command. Usingsudowill suffice):# ln -sf /usr/bin/python{3,}I'm leveraging bash's string expansion features to avoid repetition. The command effectively expands to:
# ln -sf /usr/bin/python3 /usr/bin/pythonThis is probably recommended for the sake of portability (when it comes to scripting).
The latter alternative might work up until python gets updated, replacing the default
pythoninterpreter again withpython2. @kichik pointed out the use ofalternatives(8)to adequately (and truly persistently) configure yourpythoninterpreter.As per this answer, you can issue the following commands to install and configure your default
pythoninterpreter:# alternatives --install /usr/bin/python python /usr/bin/python2 50 # alternatives --install /usr/bin/python python /usr/bin/python3 60 # alternatives --config python There are 2 programs which provide 'python'. Selection Command ----------------------------------------------- 1 /usr/bin/python2 *+ 2 /usr/bin/python3 Enter to keep the current selection[+], or type selection number: 2 $ alternatives --display python python - status is manual. link currently points to /usr/bin/python3 /usr/bin/python2 - priority 50 /usr/bin/python3 - priority 60 Current `best' version is /usr/bin/python3. $ python Python 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
- 71
 - 5
 
- 
                    [Nick Walsh](https://stackoverflow.com/a/61165250/6506157) proposes another nifty and potentially easier alternative that comes in the form of a shell alias, masking `python3` behind the `python` alias. This differs from the symbolic link approach which destructively replaces the default python interpreter system-wide. This approach is user-specific. I'm guessing that, in this case, it isn't very important. His answer also includes a terribly helpful question! Be sure to check that out. – rgcv Apr 12 '20 at 00:14
 - 
                    1[alternatives](https://stackoverflow.com/a/48757130/492773) is the more official way of doing this. Otherwise your symlinks may be overwritten by upgrades. – kichik Apr 12 '20 at 03:33
 - 
                    Ahh, I was unaware of this! That's probably the best option to configure it system-wide then. I'll work on adding that to my answer, thank you! – rgcv Apr 13 '20 at 12:58