IamWarmduscher
IamWarmduscher

Reputation: 955

How do I initialize / switch to python3 from python2 in terminal?

I just created my first AWS EC2 instance. I used sudo yum install python3 -y to install Python3 but when I check the version via python --version it says Python 2.7.16. How do I switch versions?

Upvotes: 0

Views: 3308

Answers (2)

rgcv
rgcv

Reputation: 71

There are a couple of ways to do this:

  1. You could explicitly request python3, invoking it as-is, instead of just python, i.e.:

    $ python3
    Python 3.7.6 (default, Feb 26 2020, 20:54:15)
    [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  2. As per @Nick Walsh's answer, you can create a shell alias(1) that just expands python to python3 by putting the following either into .profile, .bashrc, or even .bash_aliases:

    alias python=python3
    

    Granted python3 is in your PATH, this will work without a hitch, with the added benefit that this is a per user setting, meaning you won't be changing the system-wide python interpreter (since python remains pointing to /usr/bin/python2). If you'd like, you can opt for a system-wide alias as well by modifying /etc/profile or /etc/bashrc, adding the alias there.

  3. You could replace the python symlink, linking it to python3 instead.

    You can achieve this using ln(1) (pay close attention to the # vs. $ prompt, meaning you require root privileges to issue this command. Using sudo will suffice):

    # ln -sf /usr/bin/python{3,}
    

    I'm leveraging bash's string expansion features to avoid repetition. The command effectively expands to:

    # ln -sf /usr/bin/python3 /usr/bin/python
    

    This is probably recommended for the sake of portability (when it comes to scripting).

  4. The latter alternative might work up until python gets updated, replacing the default python interpreter again with python2. @kichik pointed out the use of alternatives(8) to adequately (and truly persistently) configure your python interpreter.

    As per this answer, you can issue the following commands to install and configure your default python interpreter:

    # alternatives --install /usr/bin/python python /usr/bin/python2 50
    # alternatives --install /usr/bin/python python /usr/bin/python3 60
    # alternatives --config python
    
    There are 2 programs which provide 'python'.
    
      Selection    Command
    -----------------------------------------------
       1           /usr/bin/python2
    *+ 2           /usr/bin/python3
    
    Enter to keep the current selection[+], or type selection number: 2
    $ alternatives --display python
    python - status is manual.
     link currently points to /usr/bin/python3
    /usr/bin/python2 - priority 50
    /usr/bin/python3 - priority 60
    Current `best' version is /usr/bin/python3.
    $ python
    Python 3.7.6 (default, Feb 26 2020, 20:54:15)
    [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

Upvotes: 2

Nick Walsh
Nick Walsh

Reputation: 1875

You can either invoke python 3 with python3 directly from the terminal, or create an alias by adding the following line to your ~/.bashrc or ~/.bash_aliases file:

alias python=python3

More details and troubleshooting tips available in this related question.

Upvotes: 3

Related Questions