SyntaxError: multiple statements found while compiling a single statement

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# SyntaxError: multiple statements found while compiling a single statement

The Python "SyntaxError: multiple statements found while compiling a single statement" occurs when you run multiple statements in a single statement.

To solve the error, run the statements one by one by pressing Enter after each statement.

If you paste the following code in IDLE, you'll get the error.

main.py
site = 'bobbyhadz.com' another_site = 'google.com'

syntaxerror multiple statements found while compiling single statement

To the left of each line, you will either see three angle brackets >>> or three dots ....

main.py
# ⛔️ SyntaxError: multiple statements found while compiling a single statement >>> site = 'bobbyhadz.com' ... another_site = 'google.com' ... print(site)

The first line starts the statement and begins with three angle brackets >>>.

Notice that the second and third lines start with three dots.

The second and third lines belong to the same statement.

To solve the error, you have to run each statement on a separate line by pressing Enter after each statement.

run each statement on separate line

Notice that each statement begins with >>>.

Now each line represents a separate statement and we aren't trying to run multiple statements while compiling a single statement.

All I did was press Enter after each statement.

In other words, this is correct:

main.py
# ✅ Correct >>> site = 'bobbyhadz.com' >>> another_site = 'google.com' >>> print(site)

And, this is incorrect and causes the error.

main.py
# ⛔️ SyntaxError: multiple statements found while compiling a single statement >>> site = 'bobbyhadz.com' ... another_site = 'google.com' ... print(site)

Paste code in the interactive Python interpreter often causes the issue.

# If you are on macOS or Linux, disable bracketed paste

If you are on macOS or Linux, disable bracketed paste by running the following command from your terminal.

shell
echo "set enable-bracketed-paste off" >> ~/.inputrc

disable bracketed paste on macos and linux

# Create a Python file instead of using the interactive Python interpreter

It is much better and more intuitive to create a Python file instead of using the interactive Python interpreter.

  1. Create a file called main.py.
  2. Paste the following code into the file.
main.py
site = 'bobbyhadz.com' another_site = 'google.com' print(site) print(another_site)
  1. Open your terminal in the same directory as your main.py file.

  2. Run the Python file with python main.py.

shell
python main.py # Or python3 python3 main.py # Or py (Windows alias) py main.py

run python script with python main py

Depending on your operating system, you might have to run the python3 main.py (macOS or Linux) command or the py main.py (Windows) command.

Make sure to replace main.py with the name of your Python script.

# Wrapping the code in a function to solve the error

You can also solve the error by wrapping the code in a function.

For example:

  1. Paste the following code into the Python interpreter.
main.py
def my_function(): site = 'bobbyhadz.com' another_site = 'google.com' print(site) print(another_site)
  1. Press Enter two times.

  2. Call the function with my_function().

main.py
my_function()

wrapping the code in a function

The function is considered a single statement, so you can wrap your code into a function, press Enter twice until you get three angle brackets >>> to the left and call the function with my_function().

# Make sure your code is correctly indented

Make sure your code is correctly indented.

main.py
site = 'bobbyhadz.com' if site == 'bobbyhadz.com': print('bobbyhadz') elif site == 'google.com': print('google') else: print('another')

indent code correctly

You can use the Tab key to indent your code.

If you want to read more about indentation in Python, check out the following article.

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