1

I have installed python 3.6 on my CentOS machine by following the guide at https://linuxize.com/post/how-to-install-python-3-on-centos-7/, which installs python 3 with the following:

sudo yum install rh-python36

The default python version, however, is still python 2. The guide mentions I can run python 3 by first running scl enable rh-python36 bash, and then python 3 will be used. However, this only works for that session - logging out and back in will revert back to python 2. According to How to set Python3.5.2 as default Python version on CentOS?, python 3 can be set as the default with the following:

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

However, this doesn't work for me as for some reason I don't have any python3 file in /usr/bin/ - I only have python, python2, python2.7, python2.7-config, python2-config and python-config (despite installing python 3 with yum as above).

ZhouW
  • 1,187
  • 3
  • 16
  • 39
  • you can find full path to python3 using `which python3` and then you can use this path with `ln -fs`. But Linux use Python for many elements in system and puting `python3` in place of `python` can makes problem - system may crash because some functions in `python3` works different then in `python2`. – furas Oct 14 '19 at 03:08
  • you can put `scl enable rh-python36 bash` in file `~/.bashrc` and it will run it everytime when you login. – furas Oct 14 '19 at 03:09
  • @furas I need to use python 3 for cron jobs run automatically by the server, would putting ``scl enable rh-python36 bash`` in ``~/.bashrc`` work in that case? – ZhouW Oct 14 '19 at 03:16
  • can't use use `python3 script.py` in cron ? – furas Oct 14 '19 at 03:18
  • 1
    BTW: Linux uses "shebang" (`#!`) in first line of script to inform system what program uses to run script. If you use `#!/usr/bin/env python3` in script (and set it executeble `chmod +x script.py`) then it should always use `python3` to run this script. – furas Oct 14 '19 at 03:20
  • Unfortunately on my server ``python3`` loads a different version of python (3.6.3) which has some issues on my server as opposed to the python loaded by ``scl enable rh-python36 bash`` which is 3.6.9 – ZhouW Oct 14 '19 at 03:21
  • then use full path to you python - `/full/path/to/your/python script.py` or in shebang `#!/full/path/to/your/python` – furas Oct 14 '19 at 03:22
  • @furas Thanks, I can run ``/opt/rh/rh-python36/root/usr/bin/python script.py`` (``/opt/rh/rh-python36/root/usr/bin/python`` being the location of the python that ``scl enable rh-python36 bash`` loads) – ZhouW Oct 14 '19 at 03:36

1 Answers1

2

First, run

$ scl enable rh-python36 bash

to switch python version.

Second, run

$ which python

to find out the exact path of python3.6 binary.

Third, edit ~/.bashrc.

$ vim ~/.bashrc

and add below line.

alias python='/opt....(the path you found by "which python")'

and save the file.

Jeff
  • 524
  • 5
  • 17