53

Is there a way to set the Python 3.5.2 as the default Python version on CentOS 7? currently, I have Python 2.7 installed as default and Python 3.5.2 installed separately.

I used the following commands

mv /usr/bin/python /usr/bin/python-old
sudo ln -fs /usr/bin/python3 /usr/bin/python

but after that yum gives the error.

-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

is there something I'm missing here?

NOTE: its the similar but opposite question of Linux CentOS 7, how to set Python2.7 as default Python version?

OldFart
  • 1,640
  • 11
  • 20
Muaaz Khalid
  • 2,199
  • 2
  • 28
  • 51

6 Answers6

95

If this

sudo ln -fs /usr/bin/python3.5 /usr/bin/python

doesn't work (it should)

you could just add an alias into your /home/.bashrcwith this command:

alias python="/usr/bin/python3.5"

and if this does not work either you should just use virtual env. Read this page to get started.

Liam
  • 6,009
  • 4
  • 39
  • 53
  • 1
    you are right, I tried aliasing, but the module `libtorrent` I'm installing still consider Python 2.7. because python 2.7 path is still same – Muaaz Khalid Aug 07 '17 at 09:07
  • 1
    anyway. using `sudo ln -fs /usr/bin/python3 /usr/bin/python` command, I'm able to use the Python 3.5.2 as default but I think `Yum` is somehow dependent upon 2.7 so, I can't use yum command with Python 3 – Muaaz Khalid Aug 07 '17 at 09:09
  • Thank you @Veltro I'm stuck with this problem for the last 5 hours. – Muaaz Khalid Aug 07 '17 at 09:12
  • I tried uninstalling python and got this error `Error: Trying to remove "yum", which is protected ` – Muaaz Khalid Aug 07 '17 at 09:22
  • 2
    someone says "Yum itself is implemented in Python I believe... So removing Python shoots your own foot pretty well!" here in comment https://serverfault.com/questions/844677/server-wiped-after-yum-remove-python – Muaaz Khalid Aug 07 '17 at 10:11
  • 2
    i suggest the alternatives method – OldFart Aug 19 '18 at 21:19
  • Incomplete answer, how about pip and everything else needed? – Alexis Jun 21 '19 at 06:02
  • I understand, it would have been nice to change the default environment rather than just the python binary. Thx! – Alexis Jun 21 '19 at 11:45
  • In this command "sudo ln -fs /usr/bin/python3 /usr/bin/python" should be modified according to the python version you have – Krishna Menon Jan 21 '20 at 09:23
  • @Liam when you said `alias python="/usr/bin/python3.5"` do you mean `python3.5` is a directory/folder? – Suresh Jun 22 '20 at 05:51
  • 1
    This solution is creating another problem. Yum is working on python 2 only, changing default python to 3 will crash yum. Check this out https://stackoverflow.com/questions/11213520/yum-crashed-with-keyboard-interrupt-error – edisonthk May 28 '21 at 14:35
79

I would suggest using alternatives instead.

As super-user (root) run the following:

# Start by registering python2 as an alternative
alternatives --install /usr/bin/python python /usr/bin/python2 50

# Register python3.5 as an alternative
alternatives --install /usr/bin/python python /usr/bin/python3.5 60

# Select which Python version to use
alternatives --config python

The last command will ask you to choose between registered/installed alternatives.

As always, well most of the time anyways, you can check out the manual (linux man pages) using this simple command

man alternatives

Note:

Altho this answer refers to/make use of specific Python versions, the alternatives command, it's concepts and uses remain the same regardless of version numbers. It is strongly suggested that you read/learn more about the alternatives command in order to understand how it can help you better manage and use your system. Also, there is a good chance that some will correct bad/unusual practices currently in use on their machines. I see it with a great majority of people which i introduce to the concept. Here is a link to a very good and simple explanation of the alternatives command.

OldFart
  • 1,640
  • 11
  • 20
  • 1
    What does the 50 and 60 do for alternatives? – Chris Marotta Jan 30 '19 at 00:22
  • 2
    It has to do with the "weight" of the option or the priority as the manpages says. Higher priorities take precendence if no alternative is manually selected. – OldFart Jan 30 '19 at 05:42
  • 1
    Remember that this will not do you any good if python version is hardcoded in the files (check the shebang on top...) – OldFart Oct 06 '19 at 20:27
  • 1
    This is the correct way to do it. Although symlinks "work", they bypass the alternatives layers which offer important improvements for things like Java, Python, etc with multiple active versions available. – Brad Hein Nov 08 '19 at 15:36
  • Is there a "complete" solution that will also make the headers from python3-devel package the default? – HCSF Dec 02 '19 at 03:24
  • I'm not sure i understand the comment. I get what dev headers are but i don't understand what is the idea behind having any 'default' since there is most likely nothing to be executed from the installation rpm. I could be wrong. I'm assuming that if you set the alternatives to any other python than default and build something using said python, it would use the appropriate header from it's current running version. Can you elaborate? Or better yet, ask a question with a reference to this one with more details. Maybe check PYTHONPATH, PYTHONHOME environment variables. – OldFart Dec 03 '19 at 09:34
  • As this is done, `yum` will complain like this File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax So you need to choose python2 as default. CentOS 7 is in the case. – UnixAgain Aug 14 '20 at 07:23
  • I've used simply `alternatives --set python /usr/bin/python3` and it works fine. – Romain Aug 21 '20 at 15:11
  • For this simple form to work, there must already be a set alternatives for name 'python' otherwise, nothing happens, just fails silently – OldFart Aug 29 '20 at 02:52
9

Option 1) Creating a soft link actually has a drawback. "yum" does not support Python3. so, if you still decide to go with symlink creation then you also need to update the /usr/bin/yum

ln -s /usr/bin/python3 /usr/bin/python

And update the shebang line with #!/usr/bin/python2 in /usr/bin/yum file

Option 2) use alternatives

alternatives --install /usr/bin/python python /usr/bin/python3.x 60
alternatives --config python 

Option 3) create an alias in bash_profile

alias python="/usr/bin/python3"
Dry_accountant_09
  • 1,371
  • 16
  • 15
5

As the question goes, Linux CentOS 7, how to set Python3.5.2 as default Python version?

Will like to complement @OldFart's answer( Unforunately, can't comment else I would have).

when using the install param with update-alternatives, you can set the priority in auto mode. Implicitly saying that the alternative with the highest priority will be the default alternative should no alternative have been set manually. using the above answer as an example,

update-alternatives --install /usr/bin/python python /usr/bin/python2 50

will set the python2 alternative with a priority of 50, and

update-alternatives --install /usr/bin/python python /usr/bin/python3.5 60

will set the python3.5 alternative with a priority of 60. and by default, the python 3.5 becomes the default python executable for the python command.

should you want to change your default python alternative,

update-alternatives --config python

Find this a better approach as i don't have to modify my path files.

  • 4
    as a side note, 'update-alternatives' is what you would use on debian-based distributions. redhat and related 'alternatives' is inspired from the former but are not the same. 'update-alternatives' is a symlink to 'alternatives' on redhat distributions that i believe can help smoothing out transitions from debian to redhat for server admin or the likes. I figured it would be good to include this as the OP clearly states he rides a centos release. – OldFart Apr 15 '19 at 19:16
  • yes, you're correct. Had to search it out. Thanks for the info. – yonga springfield Oct 07 '19 at 08:41
2

Using alternatives to set default to python3.6 still breaks yum. To fix change the python interpreter to /usr/bin/python2.7 in two files.

[root@centos7 ~]# cat /usr/bin/yum
#!/usr/bin/python2.7
[root@centos7 ~]# cat /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python2.7

I found the following article to be useful: https://rakeshjain-devops.medium.com/how-to-install-python-3-on-centos-7-7-using-yum-and-source-and-set-as-default-1dee13396f7

lav
  • 51
  • 3
1

I want to provide some additional context around why yum was broken in the OP, and why I think the alternatives method is the best approach. Perhaps there are other best practices, but I've made some discoveries and would like to share my findings.

Assuming 3.5.2 was:

  1. installed separately (as suggested by OP) similar to the steps: Python Installation Procedure From Source
  2. the --prefix option for ./configure was updated from the default --prefix = /usr/local/bin to --prefix = /usr/bin/python3

The command to link 'separately installed 3.5.2' at the location /usr/bin/python3 to system python at /usr/bin/python overwrote or otherwise modified system python, breaking yum.

This approach complements @OldFart 's answer and hopefully provides some additional perspective around root cause of why a separately installed python can cause issues.

update-alternatives was a breath of fresh air for a similar problem I ran into

woodstock
  • 29
  • 4
  • Don't forget that, and again, 'update-alternatives' is a Debian-based command and is usually not present on Redhat and related distributions. I feel it needed to be clarified since the original question clearly states the OP runs a CentOS distibution. – OldFart May 23 '20 at 11:01