Skip to content
Advertisement

Getting invalid syntax running python on ubuntu

I’ve tested the script with spyder on Windows 10 and it worked perfectly. Now I’m trying to run it on an ubuntu virtual host and it’s giving me an invalid syntax error.

The python version is Python 3.5.2 (latest version I get after updating it). The Ubuntu version is

Description:    Ubuntu 16.04.7 LTS
Release:        16.04
Codename:       xenial

I know it’s outdated but I can’t update it myself…

The code snipped giving problems to begin with is just a simple:

# Begin of time period. Format: YYYY-mm-dd
date_from = f'{year}-01-01'

I’m testing it with “python my_file.py”

Advertisement

Answer

F-strings can only be used Python 3.6 and above as mentioned in the new features of Python 3.6:

New Features PEP 498 introduces a new kind of string literals: f-strings, or formatted string literals. Formatted string literals are prefixed with ‘f’ and are similar to the format strings accepted by str.format(). They contain replacement fields surrounded by curly braces. The replacement fields are expressions, which are evaluated at run time, and then formatted using the format() protocol:

To fix this you can use str.format() instead: date_from = '{}-01-01'.format(year) or you could upgrade your Python: sudo apt install python3.7

Advertisement