Post History
What goes wrong Depending on your system's history of installations and upgrades, it may not have a python command set up even though Python is installed. This is basically for historical reasons....
#2: Post edited
- ## What goes wrong
- Depending on your system's history of installations and upgrades, it may not have a `python` command set up even though Python is installed. This is basically for historical reasons.
- From late 2008 (with the [release](https://www.python.org/downloads/release/python-300/) of Python 3.0) until 2020 (with the [sunset](https://www.python.org/doc/sunset-python-2/) of the Python 2.x branch) — and even a bit beyond that — there was a transition period where many Linux systems had a Python 2.x version and a Python 3.x version installed side-by-side. Python code is generally incompatible across this divide; Python 3 changed many aspects of language syntax, and the behaviour of quite a bit of built-in and library functionality.
- Opinions differed (and changed over time) about whether `python` should run the 2.x interpreter or the 3.x interpreter, according to the apparent pragmatic needs of users of the time. Gradually consensus shifted from having `python` run 2.x, to having it run 3.x. But one other approach that has been seen is to require the user to be explicit: have separate `python2` and `python3` commands, and leave `python` undefined.
- The Debian repositories included some Python 2.x packages [until late 2022](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1027108); up-to-date systems will typically not have Python 2.x installed, nor anything depending on it (unless perhaps it's vendored to support e.g. an older version of GIMP). However, the `python` command may still be left undefined on some of these systems, along with the removal of `python2`, such that `python3` is the expected command to run Python.
- ## What to do
- <section class="notice is-warning">
- If you are trying to use someone else's script, make sure it's compatible with Python 3 first. In general, if you have a lot of old scripts lying around, you should review them first. Having them fail with "command not found" is better than trying to force Python 2 code through a Python 3 interpreter, which could cause more subtle problems. (Fixing Python 2 code to work under Python 3 is beyond the scope of this Q&A; but keep in mind that if `python` doesn't work on your system, that's partly to protect you from this situation.)
- </section>
- The error shown in the question comes from the `command-not-found` utility. It implies two reasonable courses of action:
- * For command-line use, one can simply type the command as `python3` rather than `python`. Note that this will not fix scripts that are expecting to use the `python` name.
- * Installing the `python-is-python3` package (`sudo apt install python-is-python3`) will — as the name suggests — cause the command `python` to refer to the existing Python 3.x installation.
You can also get the effect of `python-is-python3` by doing it yourself — although you may prefer to have Apt do it, so that it's aware of the changes to your system. This consists of simply making *symbolic links* in `/usr/bin`. On my system, for example, `/usr/bin/python` is a symbolic link to `/usr/bin/python3`, which in turn links to `/usr/bin/python3.12` (the latter link should have already been set up when Python was installed as part of the OS installation). So all we need is `sudo ln -s python3 /usr/bin/python python3`.- A simpler way, not requiring user rights, is to *alias* the name, for example by using `alias python=python3` in your `~/.bash_aliases`. However, this again will not fix scripts that have `python` in the shebang line.
- ## What goes wrong
- Depending on your system's history of installations and upgrades, it may not have a `python` command set up even though Python is installed. This is basically for historical reasons.
- From late 2008 (with the [release](https://www.python.org/downloads/release/python-300/) of Python 3.0) until 2020 (with the [sunset](https://www.python.org/doc/sunset-python-2/) of the Python 2.x branch) — and even a bit beyond that — there was a transition period where many Linux systems had a Python 2.x version and a Python 3.x version installed side-by-side. Python code is generally incompatible across this divide; Python 3 changed many aspects of language syntax, and the behaviour of quite a bit of built-in and library functionality.
- Opinions differed (and changed over time) about whether `python` should run the 2.x interpreter or the 3.x interpreter, according to the apparent pragmatic needs of users of the time. Gradually consensus shifted from having `python` run 2.x, to having it run 3.x. But one other approach that has been seen is to require the user to be explicit: have separate `python2` and `python3` commands, and leave `python` undefined.
- The Debian repositories included some Python 2.x packages [until late 2022](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1027108); up-to-date systems will typically not have Python 2.x installed, nor anything depending on it (unless perhaps it's vendored to support e.g. an older version of GIMP). However, the `python` command may still be left undefined on some of these systems, along with the removal of `python2`, such that `python3` is the expected command to run Python.
- ## What to do
- <section class="notice is-warning">
- If you are trying to use someone else's script, make sure it's compatible with Python 3 first. In general, if you have a lot of old scripts lying around, you should review them first. Having them fail with "command not found" is better than trying to force Python 2 code through a Python 3 interpreter, which could cause more subtle problems. (Fixing Python 2 code to work under Python 3 is beyond the scope of this Q&A; but keep in mind that if `python` doesn't work on your system, that's partly to protect you from this situation.)
- </section>
- The error shown in the question comes from the `command-not-found` utility. It implies two reasonable courses of action:
- * For command-line use, one can simply type the command as `python3` rather than `python`. Note that this will not fix scripts that are expecting to use the `python` name.
- * Installing the `python-is-python3` package (`sudo apt install python-is-python3`) will — as the name suggests — cause the command `python` to refer to the existing Python 3.x installation.
- You can also get the effect of `python-is-python3` by doing it yourself — although you may prefer to have Apt do it, so that it's aware of the changes to your system. This consists of simply making *symbolic links* in `/usr/bin`. On my system, for example, `/usr/bin/python` is a symbolic link to `/usr/bin/python3`, which in turn links to `/usr/bin/python3.12` (the latter link should have already been set up when Python was installed as part of the OS installation). So all we need is `sudo ln -s python3 /usr/bin/python`.
- A simpler way, not requiring user rights, is to *alias* the name, for example by using `alias python=python3` in your `~/.bash_aliases`. However, this again will not fix scripts that have `python` in the shebang line.
#1: Initial revision
## What goes wrong Depending on your system's history of installations and upgrades, it may not have a `python` command set up even though Python is installed. This is basically for historical reasons. From late 2008 (with the [release](https://www.python.org/downloads/release/python-300/) of Python 3.0) until 2020 (with the [sunset](https://www.python.org/doc/sunset-python-2/) of the Python 2.x branch) — and even a bit beyond that — there was a transition period where many Linux systems had a Python 2.x version and a Python 3.x version installed side-by-side. Python code is generally incompatible across this divide; Python 3 changed many aspects of language syntax, and the behaviour of quite a bit of built-in and library functionality. Opinions differed (and changed over time) about whether `python` should run the 2.x interpreter or the 3.x interpreter, according to the apparent pragmatic needs of users of the time. Gradually consensus shifted from having `python` run 2.x, to having it run 3.x. But one other approach that has been seen is to require the user to be explicit: have separate `python2` and `python3` commands, and leave `python` undefined. The Debian repositories included some Python 2.x packages [until late 2022](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1027108); up-to-date systems will typically not have Python 2.x installed, nor anything depending on it (unless perhaps it's vendored to support e.g. an older version of GIMP). However, the `python` command may still be left undefined on some of these systems, along with the removal of `python2`, such that `python3` is the expected command to run Python. ## What to do <section class="notice is-warning"> If you are trying to use someone else's script, make sure it's compatible with Python 3 first. In general, if you have a lot of old scripts lying around, you should review them first. Having them fail with "command not found" is better than trying to force Python 2 code through a Python 3 interpreter, which could cause more subtle problems. (Fixing Python 2 code to work under Python 3 is beyond the scope of this Q&A; but keep in mind that if `python` doesn't work on your system, that's partly to protect you from this situation.) </section> The error shown in the question comes from the `command-not-found` utility. It implies two reasonable courses of action: * For command-line use, one can simply type the command as `python3` rather than `python`. Note that this will not fix scripts that are expecting to use the `python` name. * Installing the `python-is-python3` package (`sudo apt install python-is-python3`) will — as the name suggests — cause the command `python` to refer to the existing Python 3.x installation. You can also get the effect of `python-is-python3` by doing it yourself — although you may prefer to have Apt do it, so that it's aware of the changes to your system. This consists of simply making *symbolic links* in `/usr/bin`. On my system, for example, `/usr/bin/python` is a symbolic link to `/usr/bin/python3`, which in turn links to `/usr/bin/python3.12` (the latter link should have already been set up when Python was installed as part of the OS installation). So all we need is `sudo ln -s python3 /usr/bin/python python3`. A simpler way, not requiring user rights, is to *alias* the name, for example by using `alias python=python3` in your `~/.bash_aliases`. However, this again will not fix scripts that have `python` in the shebang line.
