How to change default Python version on Linux/Fedora 28

Bartosz
Junior Dev Diaries

--

Following Guido van Rossum’s post on python.org, the life of Python 2.7 is about to end. I believe that there are still some people using this version of the language or it’s set as default on their machines. Just like in my case. After some basic research the solution is not that complicated. In this article I will show you how to change default Python version on Fedora.

Before you process, please be aware of the fact, that a lot of Linux distributions like Ubuntu, Debian or RHEL (Fedora, CentOS) are still largely dependant on Python 2.7 with various applications and commands.

Local setup

Let’s start with checking our default version of Python. We can do it with this command:

# python -V
Python 2.7.15

Or just type python to see the top line of the output:

Python 2.7.15 (default, Oct 15 2018, 15:34:50)

Now, what we need to do is to check what other versions of Python we have installed on our system. To do so, please type as follows:

It is a good idea to change Python version per user. We can change it also globally, what I’ll do later in this article. Now let’s focus on local environment. We can simply create alias in our home directory:

$ alias python='/usr/bin/python3.6'
$ python --version
Python 3.6.8

Global setup

That’s it. Pretty easy, right? Now let’s have a deeper look at changing Python version globally. To check if Python alternative version is already registered, use alternatives command (here you can find a fair example of what alternatives are):

# alternatives --list | grep -i python

If your output is empty, it simply means that you have no alternative Python version configured yet. To register two Python versions, that we listed earlier in this article, use these commands:

# alternatives --install /usr/bin/python python /usr/bin/python3.6 2
# alternatives --install /usr/bin/python python /usr/bin/python2.7 1

The above lines will instruct alternatives command to create relevant symbolic links to be used any time a python command is executed. Giving python3.6 a higher priority means that it will be used as default. Let’s check:

# python --version
Python 3.6.8

If you would like to change it, use this command:

# alternatives --config pythonThere is 2 program that provides 'python'.

Selection Command
*+ 1 /usr/bin/python3.6
2 /usr/bin/python2.7

Enter to keep the current selection[+], or type selection number:

WARNING!

As mentioned in the beginning of this article, please be really careful with changing default Python.

Although warnings about python 2.x EOL are showing more and more often. For more detailed information check here.

Conclusion

My suggestion here is to think globally act locally. Whatever project we are working on at the moment or planing to do so, I recommend to use virtual environment or other tools to separate specific project from the global configurations. Otherwise, just like me, we’ll spent much more of our precious time on setting things up, instead on our code practice.

Think globally, act locally.

--

--

Bartosz
Junior Dev Diaries

I’m in the beginning of my developer’s adventure. I’m spending most of the time coding with Python and Django. Working with git and Fedora