Bizarre python error when command isn't found

Hi,

I just decided to install Tumbleweed on my new PC (my first desktop PC actually, only ever had laptops to call my own), and one thing I noticed very early on was this error message:


  File "/usr/bin/command-not-found", line 15
    print(file=sys.stderr)
              ^
SyntaxError: invalid syntax

which didn’t occur with my last Tumbleweed install. This happens whenever I type in a command that is not found. Is this is a known bug that is on its way out (thanks to someone swatting it) or is this something I need to file a bug report on?

Thanks for your time,
Brenton

If your python script is publicly available or you don’t mind posting, it should be posted for others to inspect.
And, as per the error, line 15 should be inspected.

TSU

Your python version setup is borked. Well, incomplete, at least. The command-not-found script is written for python3. The error shows that your system is trying to run it with python2. Check the symlink /usr/bin/python3, it should point to /usr/bin/python3.6. If it does not, fix the symlink. If the target is not there, (re-)install python3-base via zypper.


$ head -n 1 /usr/bin/command-not-found
#!/usr/bin/python3
$ which python3
/usr/bin/python3
$ file /usr/bin/python3
/usr/bin/python3: symbolic link to python3.6
$ rpm -q --whatprovides /usr/bin/python3.6
python3-base-3.6.5-3.3.x86_64

# trigger error by forcing cnf to python2
$ python2 /usr/bin/command-not-found
  File "/usr/bin/command-not-found", line 15
    print(file=sys.stderr)
              ^
SyntaxError: invalid syntax

This is not about the OP’s own script, but about the /usr/bin/command-not-found script.

I am having the same problem:


head -n 1 /usr/bin/command-not-found
#!/usr/bin/python3


/usr/bin/python3: symbolic link to python3.6

echo $SHELL
/bin/zsh

which python    
/usr/bin/python

which python3
/usr/bin/python3

LSB Version:    core-2.0-noarch:core-3.2-noarch:core-4.0-noarch:core-2.0-x86_64:core-3.2-x86_64:core-4.0-x86_64:desktop-4.0-amd64:desktop-4.0-noarch:graphics-2.0-amd64:graphics-2.0-noarch:graphics-3.2-amd64:graphics-3.2-noarch:graphics-4.0-amd64:graphics-4.0-noarch

Similar problem using openSUSE Leap 15.0 GNOME Version 3.26.2

.

Perhaps this is a problem with the zsh shell in Tumbleweed? I can’t reproduce the error in Bash, only in zsh:

 code
% echo $0                                                                                                                ckglxe95@localhost
zsh
 code
% fdsafdsa                                                                                                               ckglxe95@localhost
  File "/usr/bin/command-not-found", line 15
    print(file=sys.stderr)
              ^
SyntaxError: invalid syntax
 code
% bash                                                                                                                   ckglxe95@localhost
ckglxe95@localhost:~/code> echo $0
bash
ckglxe95@localhost:~/code> dfasdfas
If 'dfasdfas' is not a typo you can use command-not-found to lookup the package that contains it, like this:
    cnf dfasdfas
ckglxe95@localhost:~/code> 


Maybe this will help?

Cheers

Hi welcome, thanks for stepping in. Can’t reproduce in bash either. If this is the culprit, the OP definitely should have told us, bash is the default on openSUSE

It turns out that this is a result of the zsh/bash handler for the command-not-found program in Tumbleweed that is run with /usr/bin/python even though the script denotes it needs to use python3. (This leaves discrepancy between the system using python2 or python3, depending on what is default). This was fixed in an update, so for anyone having issues with this still, the best course of action is to

sudo zypper dup

.

Cheers

Source: https://github.com/openSUSE/scout/pull/14

The “zsh: command not found: python” error occurs when you attempt to execute the “python” command in the Zsh shell, but it cannot find the Python interpreter in your system’s PATH. To fix this, you need to ensure that Python is installed on your system, and its executable is included in your PATH. You can check if Python is installed by running “python --version” or “python3 --version” in your terminal. If it’s not installed, download and install Python from the official website. Once installed, update your PATH variable to include the directory where Python is installed. You can do this by adding the following line to your shell configuration file (e.g., ~/.zshrc): “export PATH=/path/to/python:$PATH,” where “/path/to/python” is the actual path to your Python executable. Then, run “source ~/.zshrc” to apply the changes, and you should be able to use the “python” command without encountering the error.

Are you aware of the fact that this is a thread from 2018 (five! years ago)?

Things changed considerable since. Not at least the versions of openSUSE. And also some of the people there we haven’t heard of for years.

You are welcome to the openSUSE forums. But if you have a question/problem, then please start a new topic in the most appropriate Category. And give it a good title so that it’s keywords will draw the attention of those people you need as helpers.

This thread will be closed.