Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Python3.5.2 as default Python version on CentOS?

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?

like image 571
Muaaz Khalid Avatar asked Aug 07 '17 08:08

Muaaz Khalid


People also ask

What is the default version of Python in CentOS?

The CentOS 7 Linux distribution includes Python 2 by default. However, Python 2 is going to reach EOL on January 1, 2020. While some legacy applications might require access to Python 2 for various reasons, it's vitally important to kick start new projects in Python 3.


3 Answers

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.

like image 118
Liam Avatar answered Oct 17 '22 11:10

Liam


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.

like image 20
OldFart Avatar answered Oct 17 '22 13:10

OldFart


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"
like image 6
Dry_accountant_09 Avatar answered Oct 17 '22 13:10

Dry_accountant_09