[Tutor] Need help in understanding why I'm now getting a SyntaxError on a previously working code

dn PythonList at DancesWithMice.info
Mon Jun 26 04:32:16 EDT 2023


On 26/06/2023 16.58, Joe Huang wrote:
> This is my first post on tutor at python.org. I have 30+ years in 
> scientific programming, but it was all in Fortran and awk, never in 

Welcome! Yes, many of us have fond memories of FORTRAN. Indeed there are 
places where Python and FORTRAN interact.


> Unfortunately, when I tried using it again today, I got this SyntaxError 
> that's got me stumped:
> 
>    File "wkg_API_points.py", line 33
>      filename = f"{locname}_{latitude}_{longitude}_{start}-{end}_CERES.txt"
>                                                         ^
> SyntaxError: invalid syntax
> 
> My suspicion is that I might be using an outdated version, since I dimly 
> remember having to update python before I ran the script back in April.  
> But then I don't understand why the python I'm now calling has suddenly 
> gone back to an earlier version, i.e., Python 2.7.  Since I'm very 
> unfamiliar with python syntax, I thought I'd best ask someone who knows 
> python what the problem really and how I can fix it.

You are exactly right: F-strings only arrived in Python with v3.6.

Open a terminal and at the command-line type "python". Version 
information is normally output, eg

dn $ ... python
Python 3.11.3 (main, May 24 2023, 00:00:00) [GCC 12.3.1 20230508 (Red 
Hat 12.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>>

(the quit-command is: exit()


NB the commands to start Python vary according to OpSys (and maybe even 
how it was installed. Given that you are talking of Python 2, please try 
a command such as:

python3
or
py3

and see if that starts a different Python interpreter...

If it is not possible/easy/sensible to update the Python interpreter 
currently running on your machine, The may be alternative to installing 
it in 'user-space' (cf part of the OpSys) or perhaps to set it up in a 
virtual-machine or "container".


> The complete code is attached along with a sample input file with 7 
> locations.  If anyone's curious what this code is supposed to do, it 
> retrieves synthetic weather data for any location on earth just by 
> giving the latitude, longitude, and time period.

Coincidentally, was just designing an assignment in the behavior of 
(Python) objects today, which involves correctly handling lat-long 
location-data...

> I see two possible options to solve this problem:  (1) if it is a 
> problem due to the python version,  I would need to find what happened 
> to my Python 3.X ?  (2) if it's a generic syntax error, please tell me 
> what that line should be?

I can't believe that down-grading existing code to Python 2 is a 
sensible idea. For example, every single print() call (Python 3) would 
have to be changed into a Python 2 print-statement. Plus, only for the 
short period until your son returns...

-- 
Regards,
=dn


More information about the Tutor mailing list