Python Forum

Full Version: compiling but not running source code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
if it's a function i can just run the "python3" command on that source file and i will get a failure and a message if there is a syntax error. but for commands, if there is no error, then the command is run. i want to do a "syntax check" on a big collection (over 1500) of python source files for my upgrade to 3.8 and i don't want to run any of those checked scripts.

i was thinking of making a script to do this by making a copy of each python script with the line "exit(36)\n" prepended to each, in a temporary file before running python3 on that temporary file. that way, everything will always exit with a specific exit value, instead or running any of the original source code, if it has no errors.

or is there a better way to do this like a python3 command option?
Use the builtin function compile(). It does just that: compile and not run.

From the command line, you can also try python -m compileall <options> <args>.