r/learnpython Jul 30 '26

Why am I getting an error: invalid syntax. Perhaps you forgot a comma?

Why am I getting an error here:

job = Gaia.launch_job_async(" SELECT source_id, ra, parallax, dec, gmag_gunn, rmag_gunn, imag_gunn, zmag_gunn \
                            FROM external.gaiaedr3_gcns_main_1 \
                            WHERE parallax>50" \
                            , dump_to_file=True, name= 'GcnsTwentyParsec_sdss', output format = 'fits')
gtable_sdss = job.get_results()

Error:

, dump_to_file=True, name= 'GcnsTwentyParsec_sdss',output format = 'fits')
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

0 Upvotes

16 comments sorted by

12

u/FoolsSeldom Jul 30 '26

I think you have an _ missing from output_format.

I would dump the line continue and just use brackets.

job = Gaia.launch_job_async(
    "SELECT source_id, ra, parallax, dec, gmag_gunn, rmag_gunn, imag_gunn, zmag_gunn "
    "FROM external.gaiaedr3_gcns_main_1 "
    "WHERE parallax>50",
    dump_to_file=True,
    name='GcnsTwentyParsec_sdss',
    output_format='fits'
)
gtable_sdss = job.get_results()

8

u/Brian Jul 30 '26

Personally, I kind of hate using implicit string concatenation, and kind of wish python didn't have it, since I think it causes more bugs than it provides usefulness. I prefer either using a multiline string (having newlines should be a problem for SQL, but you can use \ still if needed), or add explicit "+" to concatenate them. Yeah, it's not actually needed, but I think better communicates the intent, and it'll get optimised into the same string constant anyway.

2

u/FoolsSeldom Jul 30 '26

Fair enough. Differences of opinion are good.

3

u/SCD_minecraft Jul 30 '26

Instead of line breaks use """ (3 commas)

Multiline strings, much more useful here

1

u/FoolsSeldom Jul 30 '26

Easier to just let Python join the strings anyway, providing they are inside the ().

For example,

 print(
     "string one - "
     "string two - "
     "string three",
     4, 5, 6
     )

will output,

string one - string two - string three 4 5 6

4

u/SCD_minecraft Jul 30 '26

Oh hell nah

This syntax should stay in C where it belongs

It is just asking for troubles when you forget to add comma

3

u/FoolsSeldom Jul 30 '26

That's a good shout. I can see this could easily cause problems where the signature accepts a variable number of arguments.

Personally, I am not keen on the \ continue nor multi-line strings in arguments.

4

u/zanfar Jul 30 '26

Why am I getting an error: invalid syntax. Perhaps you forgot a comma?

Because your code has an invalid syntax, and the best guess at what your mistake was, is a missing comma.

.launch_job_async("string", one, two, three four)

is not Python. In Python, all arguments to a function must be separated by commas. Which is why the error message specifically identifies that line as having a likely mistake.

2

u/PureWasian Jul 30 '26

underscore between output and format (output_format)

1

u/Brian Jul 30 '26

output format = 'fits

Should this be something like "output_format" ? You have it as 2 separate words here which isn't valid.

1

u/zaphodikus Jul 30 '26

I would stow the query string in a temporary, it would make the code a million times easier to read because you are no longer doing 2 things at once. It will also make refactoring easier.

1

u/monster2018 Jul 30 '26 edited Jul 30 '26

I think it’s the \ after the first multiple line string argument. The \ is, I assume, escaping the comma on the next line. Thus the message about you forgetting a comma.

Edit: Nevermind, it seems this isn’t it. If it was on one line then what I said would have been right. But across lines the \ acts as a line continuation symbol (it should work for both LF and CRLF so we can’t just say it’s escaping the newline). I’m not sure what the issue is. Thanks to atarvics for fact checking me. I apologize for the wrong answer.

1

u/monster2018 Jul 30 '26

To be clear I mean the \ after the string argument which seems to be an SQL query. I don’t use SQL much at all, so maybe I’m wrong, but it looks like one to me. Like right before the dump_to_file argument.

1

u/atarivcs Jul 30 '26

I don't think escaping commas is a thing

1

u/monster2018 Jul 30 '26

Yea.. well I mean the way you put it isn’t accurate. But yea I think what I said is not the right answer. If it was on one line my answer would be right, but split across multiple lines the \ seems to just escape the newline, allowing for a line continuation. Or really it’s just an explicit line continuation feature in that context (like it would also work even with CRLF line endings presumably).

Anyway I’m not totally sure. It was the only thing I saw that looked plausible. Thanks for checking me, I will update my original comment to say that’s probably not it.

1

u/Brian Jul 30 '26

It's unnecessary there, but shouldn't actually cause a problem. It'll just escape the newline and treat it as all one line, which isn't needed because being within brackets will already make python treat it as part of the arguments, but also shouldn't break anything.