Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

more descriptive error message than "SyntaxError: invalid syntax"

learning python, coming from a php background. Keeping it short. Is there a way to get more descriptive error messages about the syntax error? like in php. Example: print var should give something like "expecting ( " , instead of the standard "SyntaxError: invalid syntax"

Thanks

like image 862
gyaani_guy Avatar asked Jul 02 '11 21:07

gyaani_guy


People also ask

How do I fix SyntaxError invalid syntax?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

Why does Python keep saying invalid syntax?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.

How do you increase SyntaxError in Python?

Raised when the parser encounters a syntax error. This may occur in an import statement, in an exec statement, in a call to the built-in function eval() or input(), or when reading the initial script or standard input (also interactively).

What is syntax error in Python with example?

Syntax errors are mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Common Python syntax errors include: leaving out a keyword.


1 Answers

My advice would be to use a solid IDE. I only tried a few before settling down for Aptana studio which is free (as in freedom) and cross-platform, but I am sure that many other offer similar functionality.

IDE with syntax highlighting will try to parse your code even before runtime, and will signal you any mistake (sometimes they have "false positives" but this is the exception, they are normally very accurate).

Here's a screenshot that illustrate how it works.

This is the wrong code:

enter image description here

And this is the popup that shows when you hover over the red X mark.

enter image description here

As delnan correctly pointed out in the comments to your question, there are a lot of possible "right" things that can go after a print, hence the verbosity of the popup. Nevertheless I find this feature very useful, as it also shows you methods from unimported objects, variables referenced before assignment and so on and so forth.

HTH!

like image 66
mac Avatar answered Oct 19 '22 09:10

mac