0 votes
1 view
in Python by (47.8k 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 (107k 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')

Welcome to Intellipaat Community. Get your technical queries answered by top developers !