Check the syntax of a Python script without executing it

avatar
Borislav Hadzhiev

Last updated: Jun 13, 2023
5 min

banner

# Table of Contents

  1. Check the syntax of a Python script without executing it
  2. Using the compileall command to check the syntax of all Python scripts in a directory
  3. Using a custom Python script to check the syntax of other Python scripts
  4. Using the ast module to check the syntax of a Python script without running it
  5. Check the syntax of a Python script using PyFlakes
  6. Looking for a linter? Check out Pylint

# Check the syntax of a Python script without executing it

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.

main.py
if 10 > 5: print('bobbyhadz.com')
The code for this article is available on GitHub

You can check the syntax of the script without executing it by running the following command in your terminal.

shell
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.

check syntax of python script without executing it

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.

main.py
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.

shell
python -m py_compile main.py # or with python3 python3 -m py_compile main.py

check syntax of python script error shown

Running the command produces the following output:

shell
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.

shell
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:

main.py
if 10 > 5: print('bobbyhadz.com')

And the code for the another.py file:

another.py
if 5 == 5: print('google.com')

check syntax of multiple python scripts

The code for this article is available on GitHub

If I comment out the print() call in another.py:

main.py
if 5 == 5: # print('google.com')

Rerunning the command shows that the syntax error is caught.

syntax error caught in second file

# Using the compileall command to check the syntax of all Python scripts in a directory

If 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.

shell
python -m compileall -q . # or python3 python3 -m compileall -q .

check syntax of all python scripts in directory

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.

src/another.py
if 5 == 5: # print('google.com')

The file has a syntax error, so let's try to rerun the compileall command.

shell
python -m compileall -q . # or python3 python3 -m compileall -q .

syntax errors in subdirectories are also caught

You can also specify a directory when running the command.

shell
python -m compileall -q src # or python3 python3 -m compileall -q src

The command compiles the files in a directory called src.

# Using a custom Python script to check the syntax of other Python scripts

You can also use a custom Python script to check the syntax of other Python scripts.

  1. Save the following code in a file named check_syntax.py.
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')
  1. Now you can use the module as follows:
shell
python check_syntax.py main.py # or with python3 python3 check_syntax.py main.py
The code for this article is available on GitHub

Here is the example code for main.py.

main.py
if 10 > 5: print('bobbyhadz.com')

check syntax using custom python script

If I now change the code in main.py so it has a syntax error:

main.py
if 10 > 5: # print('bobbyhadz.com')

And rerun the command:

custom script catches error

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.

another.py
if 5 == 5: print('google.com')

Now let's pass both files to the command.

shell
python check_syntax.py main.py another.py # or with python3 python3 check_syntax.py main.py another.py

passing multiple files to command

# Using the ast module to check the syntax of a Python script without running it

You can also use the ast (Abstract Syntax Tree) module to check the syntax of a Python script without running it.

shell
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.

check syntax of python script using ast

If there are no syntax errors in the file, the command produces no output.

check syntax of python script without errors

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.

check_syntax.py
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}')
The code for this article is available on GitHub

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.

shell
python check_syntax.py # or with python3 python3 check_syntax.py

check syntax using ast no errors

If the file contains errors, they are printed to the terminal.

check syntax using ast with errors

# Check the syntax of a Python script using PyFlakes

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.

shell
pip install --upgrade pyflakes # or with pip3 pip3 install --upgrade pyflakes

Now use the module to check for syntax errors.

shell
pyflakes main.py

Make sure to replace main.py with the name of your actual file.

check for errors using pyflakes

If the file doesn't contain any errors, no output is printed.

pyflakes no errors

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.

# Looking for a linter? Check out Pylint

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:

  • checks for errors
  • enforces a coding standard
  • looks for code smells
  • makes suggestions on how the code should be refactored
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.