Last updated: Jun 13, 2023
Reading time·5 min
You can check the syntax of a Python script without executing it by compiling the file.
The py_compile
module will compile the specified files and will alert you if
you have syntactical errors.
Suppose you have the following Python script named main.py
.
if 10 > 5: print('bobbyhadz.com')
You can check the syntax of the script without executing it by running the following command in your terminal.
python -m py_compile main.py # or with python3 python3 -m py_compile main.py
Make sure to replace main.py
with the name of your Python script.
As shown in the screenshot, no errors are reported because the syntax in the
main.py
file is valid.
Let's comment out the call to the print function.
if 10 > 5: # print('bobbyhadz.com')
The file has a syntactical error because we didn't add any code to the if
block and didn't use a pass statement.
Let's run the py_compile
command again.
python -m py_compile main.py # or with python3 python3 -m py_compile main.py
Running the command produces the following output:
Sorry: IndentationError: expected an indented block after 'if' statement on line 1 (main.py, line 2)
When the py_compile module is run as a script, it compiles all of the specified files.
If one or more of the files cannot be compiled, a non-zero exit status is returned.
You can pass as many Python scripts to the py_compile
command as necessary.
python -m py_compile main.py another.py # or with python3 python3 -m py_compile main.py another.py
The command checks the syntax of the main.py
and another.py
files.
The code for the main.py
file:
if 10 > 5: print('bobbyhadz.com')
And the code for the another.py
file:
if 5 == 5: print('google.com')
If I comment out the print()
call in another.py
:
if 5 == 5: # print('google.com')
Rerunning the command shows that the syntax error is caught.
compileall
command to check the syntax of all Python scripts in a directoryIf you need to check the syntax of all Python scripts in a directory, use the
python -m compileall
command.
For example, to check the syntax of the Python scripts in the current directory, use the following command.
python -m compileall -q . # or python3 python3 -m compileall -q .
When the -q
option is set, the list of files is not printed.
Note that the command will also report syntax errors in files contained in subdirectories.
For example, suppose we have an src
directory that contains a file called
another.py
.
if 5 == 5: # print('google.com')
The file has a syntax error, so let's try to rerun the compileall
command.
python -m compileall -q . # or python3 python3 -m compileall -q .
You can also specify a directory when running the command.
python -m compileall -q src # or python3 python3 -m compileall -q src
The command compiles the files in a directory called src
.
You can also use a custom Python script to check the syntax of other Python scripts.
check_syntax.py
.import sys file_names = sys.argv[1:] for file_name in file_names: source = open(file_name, 'r', encoding='utf8').read() + '\n' compile(source, file_name, 'exec')
python check_syntax.py main.py # or with python3 python3 check_syntax.py main.py
Here is the example code for main.py
.
if 10 > 5: print('bobbyhadz.com')
If I now change the code in main.py
so it has a syntax error:
if 10 > 5: # print('bobbyhadz.com')
And rerun the command:
It catches the syntax error without running the file.
You can also pass multiple files to the command.
Here is the example code for another.py
.
if 5 == 5: print('google.com')
Now let's pass both files to the command.
python check_syntax.py main.py another.py # or with python3 python3 check_syntax.py main.py another.py
ast
module to check the syntax of a Python script without running itYou can also use the ast (Abstract Syntax Tree) module to check the syntax of a Python script without running it.
python -c "import ast; ast.parse(open('main.py').read())" # or with python3 python3 -c "import ast; ast.parse(open('main.py').read())"
Make sure to replace main.py
with the name of your actual file.
If there are no syntax errors in the file, the command produces no output.
You can also define a custom Python script if you don't want to use a command.
Create a file called check_syntax.py
and add the following code to it.
import ast import traceback file_name = 'main.py' with open(file_name, 'r', encoding='utf-8') as file: source = file.read() is_valid = True try: ast.parse(source) except SyntaxError: is_valid = False traceback.print_exc() print(f'Valid syntax: {is_valid}')
Make sure to update the value of the file_name
variable to specify the name of
the file you want to check.
Run the file with python check_syntax.py
.
python check_syntax.py # or with python3 python3 check_syntax.py
If the file contains errors, they are printed to the terminal.
You can also use the PyFlakes module to check the syntax of a Python script without executing it.
First, open your terminal and install the module.
pip install --upgrade pyflakes # or with pip3 pip3 install --upgrade pyflakes
Now use the module to check for syntax errors.
pyflakes main.py
Make sure to replace main.py
with the name of your actual file.
If the file doesn't contain any errors, no output is printed.
You can read more about using the PyFlakes module on the package's GitHub page.
The module checks Python source files for errors.
The module works by parsing the source file, not by importing it.
Therefore using the module with files that have side effects is safe.
If you are looking for a linter, I'd recommend you check out Pylint.
Pylint is a static code analyzer that analyses your code without actually running it.
The module:
You can learn more about the related topics by checking out the following tutorials:
--no-cache-dir
option