Explore Courses Blog Tutorials Interview Questions
0 votes
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

Another thing you can do is by writing the following code and after writing the code you will save the code as and use the python 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