Search code examples
pythonlinuxraspberry-piraspbianraspbian-stretch

How to remove default Python, install latest version on Raspbian


I just got my first Raspberry Pi, and I wanted to use the latest version of Python, but I don't know how to set it up so that when I run python in the command line, it will run Python 3.8.2, rather than 3.7.3. I downloaded the latest release of Python from the website, but it's not a setup script, and I'm not sure how to set it to the path otherwise. Is there a way to replace the default version on Raspbian with the version I've just downloaded?

This is my first time working with any kind of Linux distro, so when I look for the answer on SO or elsewhere, I don't quite know how to word it, and there are very different answers, which makes me think I'm not asking the right question. What could I be doing wrong, and how can I set it up correctly?

Thanks.


Solution

  • First Way: You can change Python 3.8.x as the default to Python 3.8.x.

    As you said that you have already installed the latest version.Right after that,
    Add Python3.8.x & Python 3.8.x to update-alternatives

    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8.0 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8.2 2
    

    Update Python 3 to point to Python 3.8.x

    sudo update-alternatives --config python3 Enter 2 for Python 3.8.2 or 1 for 3.8.0

    python3 --version
    Python 3.8.2 
    

    Second Way: Find out what python binary executables are available on your system.

    $ ls /usr/bin/python*
    /usr/bin/python3.8.0  /usr/bin/python3.8.2 
    

    To Change python version:
    Create an alias within user's home directory. Open ~/.bashrc file and add new alias to change your default python executable:

    alias python='/usr/bin/python3.8.2'
    

    Once you make the above change, re-login or source your .bashrc file:

    $ . ~/.bashrc
    

    Check your default python version:

       $ python --version
        Python 3.8.2