Syntax error match statement with python310 / python312

I installed python310 only, and later python312 only.

Unfortunately both python versions do not recognize the “match” statement, resulting in a syntax error.

type python3.12+enter in terminal window, then type ‘match smth:’+enter in python312 CLI to check it out

Can you provide an example of how you’re trying to use it?

Show everything - the code, the command you’re running to execute the code, and the output. Please use preformatted text (the </> button in the editor toolbar).

Sounds like a programming question, so I’m going to move this to the programming category.

The syntax error comes when you improperly call the python version.

With a valid python script from as example Python Match Case Statement - GeeksforGeeks .
The match case is supported from python 3.10 onwards.

Wrong call of python version:

test@leaptest:~> python3 /home/test/test.py 
  File "/home/test/test.py", line 6
    match num:
            ^
SyntaxError: invalid syntax
test@leaptest:~> 

Correct call of python312 version (same python script as above):

test@leaptest:~> python3.12 /home/test/test.py 
Enter a number between 1 and 3: 2
Two
test@leaptest:~> 

Correct call of python310 version (same python script as above):

test@leaptest:~> python3.10 /home/test/test.py 
Enter a number between 1 and 3: 7
Number not between 1 and 3
test@leaptest:~> 

The above tests were performed on a Leap 15.6 system. Works out of the box.

If the python version is called correctly, but it still throws a syntax error, then it is what it is: a syntax error in the python script.

1 Like

i’m not familiar with python at all. also not aware of any environment settings I need to do after python package install.

I’m just trying to run a script from github.

this script has run without errors in Fedora 40. I did not had to install any python package.

however on Leap I installed the prerequisite package python310. and run ./install.sh and the syntax error pops up.

Do yourself a favor and re-write the code in perl or bash. In my experience, Python is utterly unreliable. 3.10 vs 3.11 and everything breaks. And the enforced object oriented is just dumb. I don’t need the overhead of OO for a simple script!

Gives a HTTP error 404.

Very good question!

To the OP, are you aware of this: How To Ask Questions The Smart Way ?

@oxwrongagain Great technical statement…

@ed61 The small github script is an example for bad referencing of the python version. These devs want to run following snippet:

[[ $INVERT =~ ^[Yy]$ ]] && python3 cros-keyboard-map.py -i || python3 cros-keyboard-map.py

But this wont work on openSUSE as the python version is coded into the name (python3.10, python3.12). That means calling python3 wont work with the match statement, as this function is only supported from python3.10 onwards.

So the devs of this github project didn’t make it useable on many linux distributions, as they differ in naming their python packages. So it is not yours or openSUSEs fault, but from the devs of this project…

You may download the script and adapt all the questionable python calls in the script and try to run it afterwards. The chances are high that it might work…

2 Likes

i moved from leap to tumbleweed. i chose gnome as desktop.

before i executed this github script, i checked “sudo zypper se python*” and python311 was pre-installed.

then I executed the script without errors.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.