Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Why is "'python' not found" on Debian/Ubuntu/Mint/etc. ?

+4
−0

This question is motivated by several threads and posts on the Linux Mint forums in which people encounter this sort of problem.

On Debian-derived distributions of Linux, it's possible that the python command described in tutorials may not work out of box:

$ python /path/to/tutorial/folder/example_script.py
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3

Similarly for third-party software that suggests explicitly running a Python script to start the program, or to execute a script that has a shebang line like #!/usr/bin/python or #!/usr/bin/env python.

I had understood that Python is not only pre-installed, but critical for my system to operate properly. Why can't the python command be found, and what should I do about it?

History

1 comment thread

Self answer delays (1 comment)

1 answer

+3
−0

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 of Python 3.0) until 2020 (with the sunset 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; 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

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.)

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.

History

0 comment threads

Sign up to answer this question »