DevOps | Cloud | Analytics | Open Source | Programming

How to Check Syntax Errors in Python Code ?

In this post , we will explore - How to Check Syntax Errors in Python Code. You can use various options to check for any syntax errors in Python without executing the script . Try the below options -

Option 1 - Using Compilation Script :

  • Using Python basic compilation . Use the code below and save it as any .py file e.g

import sys
your\_python\_script\_name = sys.argv\[1\]
source = open(your\_python\_script\_name, 'r').read() + '\\n'
compile(source, your\_python\_script\_name, 'exec')

  • Next run your Python script(say against the above script


Option 2 - Using Direct Compilation :

  • You can directly compile your Python script
  • Note the below poinst -
    • python returns a non-zero exit code if the compilation fails.
    • Also type errors are not detected -- those are only detected at runtime

python -m py\_compile

Option 3 - Using Pychecker :

  • You can use PyChecker to syntax check your python code. Use code below from the command line:

    pychecker \[options\] YOUR\_PYTHON\


  • The [options] provides below features -
    • --only  --> only warn about files passed on the command line
    • -#, --limit --> the maximum number of warnings to be displayed
    • --no-shadowbuiltin --> check if a variable shadows a builtin
    • -q, --stdlib --> ignore warnings from files under standard library
    • -T, --argsused --> unused method/function arguments

  • If you want to syntax check multiple Python scripts at one go ,

pychecker <YOUR\_DIR>/\*.py

Sample how pychecker shows the syntax errors - Imported module (string) not used Instantiating an object with arguments, but no constructor Object (useless) has no attribute (valeu) No local variable (var1) self is not first method argument

Option 4 - Using Pyflakes :

  • Another worthy option to try -


Option 5 - Using "ast" module

  • The ast module helps to process trees of the Python abstract syntax grammar - basically ast helps to find out the current grammar appearance.
  • More here -
  • Using CLI

python -c "import ast; ast.parse(open('').read())"

  • Check syntax of any block of code

import ast
tree = ast.parse("print ('This is Great')")
"Module(body=\[Expr(value=Call(func=Name(id='print', ctx=Load()), args=\[Str(s='This is Great')\], keywords=\[\]))\])"

  • Alternatively use as a python script

import ast, traceback

with open(g) as f:
  source =
valid = True


except SyntaxError:
  valid = False


Hope this write up was helpful.  

Other Interesting Reads -


python syntax check, python syntax, invalid syntax python, python for loop syntax, python if syntax, python, pycharm, django, matplotlib, python online, python programming, python list, learn python, python syntax checker,How do I check Python syntax, How do I check Python syntax online, What is Python basic syntax, What Is syntax check,check python script online,python code tester,how to find error in python code ,pep8 checker,python 3.5 syntax checker,python syntax error,python check function,bug checker python, python syntax checker command line, python 3.5 syntax checker, python syntax error fixer,syntaxerror: invalid syntax python 3,invalid syntax python print,how to resolve name error in python,invalid syntax python def,pylint syntax error,how to find error in python code,file <stdin>'', line 1 syntaxerror: invalid syntax,online python compiler, python find syntax error,How to find syntax error in python,How to solve the python syntax error,What is a syntax error in python,How does Python handle syntax errors,python syntax error,how to find error in python code,python compiler invalid syntax python ,check python script online,python code tester,occurs when a syntax error is raised for a module