How can I check the syntax of Python script without executing it? How can I check the syntax of Python script without executing it? python python

How can I check the syntax of Python script without executing it?


You can check the syntax by compiling it:

python -m py_compile script.py


import sysfilename = sys.argv[1]source = open(filename, 'r').read() + '\n'compile(source, filename, 'exec')

Save this as checker.py and run python checker.py yourpyfile.py.