Based on the example here , I tried to run the following code snippet but failed:
run_mode, num_repeats, num_runs = 'short', 5, 1000
s = f"""
{'-'*40}
# Operator Micro-benchmarks
# Run_mode: {run_mode}
# Num_repeats: {num_repeats}
# Num_runs: {num_runs}
{'-'*40}
"""
print(s)
The error is as follows:
$ python test.py
File "test.py", line 11
"""
^
SyntaxError: invalid syntax
I’m surprised at this error message because I don’t see anything wrong with my code. Any tips will be appreciated.
Regards,
Zhao
vainaixr
(:)
September 24, 2022, 7:17am
2
I ran the same code on colab, it works.
It might be a python version issue.
Works fine for me.
What version of Python are you using? I can only duplicate that error in Python 2.7. That is no longer supported and is extremely out of date. You should be using Python 3.10.
2 Likes
dstromberg
(Daniel R Stromberg)
September 24, 2022, 4:43pm
4
Hongyi Zhao:
run_mode, num_repeats, num_runs = 'short', 5, 1000
s = f"""
{'-'*40}
# Operator Micro-benchmarks
# Run_mode: {run_mode}
# Num_repeats: {num_repeats}
# Num_runs: {num_runs}
{'-'*40}
"""
print(s)
It works in CPython 3.6 and up:
$ pythons --file /tmp/foo
below cmd output started 2022 Sat Sep 24 09:40:41 AM PDT
/usr/local/cpython-0.9/bin/python (unknown) bad
Parsing error: file /tmp/foo, line 5:
s = f"""
^
Unhandled exception: run-time error: syntax error
/usr/local/cpython-1.0/bin/python (1.0.1) bad
File "/tmp/foo", line 5
s = f"""
^
SyntaxError: invalid syntax
/usr/local/cpython-1.1/bin/python (1.1) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-1.2/bin/python (1.2) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-1.3/bin/python (1.3) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-1.4/bin/python (1.4) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-1.5/bin/python (1.5.2) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-1.6/bin/python (1.6.1) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.0/bin/python (2.0.1) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.1/bin/python (2.1.0) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.2/bin/python (2.2.0) bad
File "<string>", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.3/bin/python (2.3.0) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.4/bin/python (2.4.0) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.5/bin/python (2.5.6) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.6/bin/python (2.6.9) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-2.7/bin/python (2.7.16) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.0/bin/python (3.0.1) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.1/bin/python (3.1.5) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.2/bin/python (3.2.5) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.3/bin/python (3.3.7) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.4/bin/python (3.4.8) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.5/bin/python (3.5.5) bad
File "/tmp/foo", line 13
"""
^
SyntaxError: invalid syntax
/usr/local/cpython-3.6/bin/python (3.6.13) good
----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000
----------------------------------------
/usr/local/cpython-3.7/bin/python (3.7.0) good
----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000
----------------------------------------
/usr/local/cpython-3.8/bin/python (3.8.0) good
----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000
----------------------------------------
/usr/local/cpython-3.9/bin/python (3.9.0) good
----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000
----------------------------------------
/usr/local/cpython-3.10/bin/python (3.10.0) good
----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000
----------------------------------------
/usr/local/pypy3-7.3.9/bin/pypy3 (3.9.12) good
----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000
----------------------------------------
22 bad ************************************************************
6 good ****************
1 Like
bryevdv
(Bryan Van de Ven)
September 24, 2022, 4:52pm
5
Unsurprising, f-strings were added in version 3.6.
2 Likes
tjreedy
(Terry Jan Reedy)
September 24, 2022, 11:48pm
6
Hongyi, please include python version with questions.
I don’t have the pythons
command at all:
werner@X10DAi:~$ pythons
Command 'pythons' not found, did you mean:
command 'python0' from snap python0 (0.9.1)
command 'python2' from deb python2 (2.7.18-3)
command 'python3' from deb python3 (3.10.4-0ubuntu2)
See 'snap info <snapname>' for additional versions.
dstromberg
(Daniel R Stromberg)
December 22, 2022, 2:38pm
8
1 Like
Hi Daniel R Stromberg , thank you for letting me know your wonderful tools.
Nowadays, git is the de facto standard tool for distributed code management. So, I want to know whether you have these tools resided on git based repositories?