Last updated: Apr 11, 2024
Reading time·3 min
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.
site = 'bobbyhadz.com' another_site = 'google.com'
To the left of each line, you will either see three angle brackets >>>
or
three dots ...
.
# ⛔️ 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.
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:
# ✅ Correct >>> site = 'bobbyhadz.com' >>> another_site = 'google.com' >>> print(site)
And, this is incorrect and causes the error.
# ⛔️ 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 by running the following command from your terminal.
echo "set enable-bracketed-paste off" >> ~/.inputrc
It is much better and more intuitive to create a Python file instead of using the interactive Python interpreter.
main.py
.site = 'bobbyhadz.com' another_site = 'google.com' print(site) print(another_site)
Open your terminal in the same directory as your main.py
file.
Run the Python file with python main.py
.
python main.py # Or python3 python3 main.py # Or py (Windows alias) py 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.
You can also solve the error by wrapping the code in a function.
For example:
def my_function(): site = 'bobbyhadz.com' another_site = 'google.com' print(site) print(another_site)
Press Enter
two times.
Call the function with my_function()
.
my_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.
site = 'bobbyhadz.com' if site == 'bobbyhadz.com': print('bobbyhadz') elif site == 'google.com': print('google') else: print('another')
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.
You can learn more about the related topics by checking out the following tutorials: