How to set Python3.5.2 as default Python version on CentOS?
Solution 1
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/.bashrc
with 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.
Solution 2
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.
Solution 3
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).
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.
Solution 4
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"
Solution 5
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:
- installed separately (as suggested by OP) similar to the steps: Python Installation Procedure From Source
- 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
Muaaz Khalid
Senior Software Engineer Senior Software Engineer at Freelancer.com and a Full Stack Web Developer with extensive experience in Server management, I have the strong command on following languages: Angular, PHP/Java, MySQL/MSSQL/PostgreSQL, SCSS/HTML & ofcourse Python for the Artificial Intelligence and Machine Learning. As a freelancer, My day job involves Requirement gathering & Analysis, Design & Development, Testing and deployment, I use this platform to post and answer the questions I have a MS(CS) and BSCS(Hons) in Computer Science, my MS(CS) thesis research was conducted at the National University of Computer and Emerging Sciences, Pakistan. My research interests revolved Image Classification and Information Retrieval & Text Mining.
Updated on July 09, 2022Comments
-
Muaaz Khalid almost 2 years
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?