How to switch to Python 3 from 2 on Linux

Viewed 1510

I'm operating on a Raspberry Pi through PuTTY, and I need it to use Python 3 instead of 2. I've already done alias python = python3, but that does not seem to change the default, only the keyword itself used. I want to change to 3 because for example, when I try to 'pip install selenium', I get the following prompt:

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop s
upport for Python 2.7 in January 2021. More details about Python 2 support in pi
p can be found at https://pip.pypa.io/en/latest/development/release-process/#pyt
hon-2-support

And it won't even let me pip install selenium. After trying for a minute or two, it prompts me this:

ERROR: Could not find a version that satisfies the requirement selenium (from versions: none) ERROR: No matching distribution found for selenium

My Linux machine has both Python 2 and 3 installed.

EDIT: Thank you for your contributions, I understand what you're saying. Now I have a new problem though - I broke sudo apt-get update by following someone else's suggestion. I have a post here: Raspberry Pi sudo apt-get update doesnt work

3 Answers

Looks as though you can install python3 from apt on raspbian by doing the following steps:

  1. sudo apt-get update

  2. sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev tar wget vim

  3. wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz

  4. Install the new version using the following commands, all typed in your PuTTY session:

a. sudo tar zxf Python-3.8.0.tgz

b. cd Python-3.8.0

c. sudo ./configure --enable-optimizations

d. sudo make -j 4

e. sudo make altinstall

  1. Check that the installation can be found using python3.8 -V

  2. Create a venv using python3.8 -m venv selenium-venv

  3. Activate said venv using . ./selenium-venv/bin/activate in your shell.

  4. Now it will work.

I hope that helps, leave a comment if you need further assistance.

its probably because you assigned a variable "python3" to point to python (2.7) binary. see if you made an env variable "python3":

printenv python3

if you did, unset it:

unset python3

to see where and what versions of python you have installed:

locate /bin/python

if you see "python" that is binary for 2.7, "python3" is the binary for 3.x

try to run it by typing the whole path in the terminal: e.g. : /usr/local/bin/python3

if pip is not working try to upgrade or use pip3

Python 3 should be the standard on all systems since the beginning of 2020. If you are not using some "old" software that depends on python 2 then you can remove python 2 from your OS.

sudo apt remove python2

Then there will be no confusion which python to use.

On raspbian or RaspberryOS i think they still use the versionnumber after the name. eg: pyhton3, pip3 So when you run pip you have to run pip3 check if you have it installed. You could also use python3 -m pip

Related