Another option that would work across different (and older) versions of Ubuntu is the "alternatives" system, either the command-line update-alternatives
(should be installed) or GUI galternatives
(not installed by default). It lets one configure your "preferred" version when you have multiple versions, or completely different but compatible implementations, (e.g. compilers, editors; see for example this dump: update-alternatives --get-selections
).
To see which versions of python are currently the default (note: these are symbolic links to specific versions):
$ type python python2 python3
python not found
python2 is /usr/bin/python2
python3 is /usr/bin/python3
Usually (like, always, without exception) you want the default python
to be python2
, for compatibility reasons (otherwise you will break apt and other system utilities!) and then just explicitly invoke python3
if and when you really mean Python3.
To see what current alternatives are configured for python
:
$ sudo update-alternatives --list python
error: no alternatives found
If alternatives are found, skip the following (see --config
, below). Otherwise, we can configure both python2 and python3 as "alternatives" for the python
command and you can switch between them (or just set python2
here and you're done; the rest is here for completeness & as an exercise):
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 40
using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 20
using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
$ type python
/usr/bin/python
$ python --version
Python 2.7.18
(Important! if your default python is showing as python3
, then change it here to python2
, otherwise apt
and other system utilities may be broken!)
To use update-alternatives
to change the version currently selected, do the following and choose the version you want:
$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python2 40 auto mode
1 /usr/bin/python2 40 manual mode
2 /usr/bin/python3 20 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
To completely remove all python
alternative configurations (e.g., to start over, or just undo everything if anything weird is going on):
$ sudo update-alternatives --remove-all python
python
&python3
? when they were both already installed (ie. Lubuntu 18.04 LTS comes with both though 3.6 for python3). How did you install them? and did you remove or replace the pre-installed versions? I suspect your issue is $PATH, that you removed the preinstalled versions that were in your path, installed it elsewhere or amended your $PATH to something different to default.history
can refresh your memory on how. On my Lubuntu awhereis python
tells me where it is, and the commandecho $PATH
shows the list of directories searched for commands. What I suggested may be corrupted on yours, or if you installed from source (why I asked HOW) it could be installed outside of $PATH defaults. If I enterpython --version
mine responds 2.7.16, andpython3 --version
responds 3.7.3 (note: I'm not using 18.04 so it's likely later versions). Did you remove anything? and how was it installed?python
invoke Python 3, is there a particular reason you can't just runpython3
? If there is, it might be worth explaining what that reason is.