Skip to main content

How to Resolve Python Pylance Error "Statements must be separated by newlines or semicolons"

When using the Pylance language server in VS Code for Python development, you might encounter the error message Statements must be separated by newlines or semicolons. This static analysis error indicates that Pylance has detected more than one distinct Python statement on a single line of code without the required semicolon separator. The most frequent cause is using the older Python 2 print statement syntax in a Python 3 environment.

This guide explains the Python syntax rules involved and provides clear solutions to fix this Pylance error.

Understanding the Error: Python Statement Separation

Python generally expects one logical statement per line. A statement is a complete unit of execution (e.g., an assignment x = 1, a function call my_func(), an if clause, a return).

Unlike some languages, Python does not typically require semicolons (;) at the end of statements if each statement is on its own line. The newline character itself acts as the primary statement separator.

However, Python does allow multiple simple statements on a single line if they are explicitly separated by semicolons.

Pylance reports the error "Statements must be separated by newlines or semicolons" when it finds what it interprets as two or more distinct statements on the same line without that required semicolon separator.

Common Cause 1: Using print as a Statement (Python 2 Syntax)

The most frequent trigger for this error in modern Python development (Python 3) is using the outdated Python 2 syntax for printing.

  • Python 2: print was a special statement. Syntax: print "Hello" or print variable.
  • Python 3: print became a built-in function. Syntax: print("Hello") or print(variable).

When Pylance encounters Python 2-style print syntax in a Python 3 context, like print some_variable, it often interprets print as one potential statement (or keyword/identifier) and some_variable as a separate expression/statement, leading to the error because they aren't separated correctly and print isn't being called as a function.

import json

my_data = {'city': 'London', 'country': 'UK'}

try:
# Error Scenario (in Python 3 context analyzed by Pylance)
# Pylance sees 'print' and 'json.dumps(...)' as two statements here.
# ⛔️ Statements must be separated by newlines or semicolons Pylance(reportGeneralTypeIssues)
print json.dumps(my_data)

except SyntaxError as e: # Note: Python 3 itself might raise SyntaxError here too
print(f"Caught Syntax/Pylance error related to print statement: {e}")
except NameError as e: # Or NameError if 'print' isn't recognized at all
print(f"Caught NameError related to print statement: {e}")

Solution 1: Use the print() Function (Python 3 Syntax)

The correct solution for Python 3 code is to always use print as a function, enclosing its arguments in parentheses ().

import json

my_data = {'city': 'London', 'country': 'UK'}

# ✅ Corrected Code: Use print() as a function call
print(json.dumps(my_data))
# Output: {"city": "London", "country": "UK"}

By adding the parentheses, print(...) becomes a single, valid function call statement, resolving the Pylance error and ensuring correct execution in Python 3.

note

For code explicitly needing Python 2/3 cross-compatibility, you might see from __future__ import print_function at the top of Python 2 files. This makes print behave like the Python 3 function even in Python 2.

Common Cause 2: Multiple General Statements on One Line

Less commonly, the error can occur if you simply place two unrelated valid Python statements on the same line without a semicolon.

# Error Scenario
# Two assignment statements on one line without separation
a = 10 b = 20 # ⛔️ Statements must be separated by newlines or semicolons Pylance(...)
  • Use Newlines (Recommended): The standard and most readable Python practice is to place each statement on its own line.
    # ✅ Corrected Code: Separate statements with newlines
    a = 10
    b = 20
    print(a, b) # Output: 10 20
  • Use Semicolons (Discouraged but Valid): Python allows semicolons to separate simple statements on a single line. While syntactically valid, this is generally discouraged by style guides (like PEP 8) as it often reduces readability.
    # Syntactically valid, but less readable
    a = 10; b = 20; print(a, b) # Output: 10 20

    # Another example (often seen in overly condensed code)
    value = 5
    if value > 3: print("Greater"); print("Than 3") # Less clear

    # Preferred version using newlines and indentation:
    if value > 3:
    print("Greater")
    print("Than 3")
    Only use semicolons for multiple statements on one line if there's a compelling reason and it doesn't harm clarity (which is rare). For compound statements (like if, for, def), you cannot put the entire block on one line using semicolons; they must use indentation on subsequent lines.

Conclusion

The Pylance error Statements must be separated by newlines or semicolons signals a violation of Python's basic syntax structure for separating statements.

The most common fix is:

  1. If the error involves print, ensure you are using the Python 3 function call syntax: print(...).
  2. For other statements, place each distinct statement on its own line.

While semicolons (;) can be used to separate simple statements on a single line, this is generally discouraged in favor of using newlines for better code readability and adherence to standard Python style (PEP 8).