Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I used to use perl -c programfile to check the syntax of a Perl program and then exit without executing it. Is there an equivalent way to do this for a Python script?

1 Answer

0 votes
by (106k points)

If you want to check the syntax of Python script without executing it then you can use the following command:

python -m py_compile script.py

Another thing you can do is by writing the following code and after writing the code you will save the code as checker.py and use the python checker.py yourpyfile.py. the command to run the code without executing.

import sys

filename = sys.argv[1]

source = open(filename, 'r').read() + '\n'

compile(source, filename, 'exec')

Browse Categories

...