Skip to content

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

Fix the Pylance error in VS Code by converting Python 2-style print statements to Python 3's print() function or separating multiple statements with 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:
    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'}

print(json.dumps(my_data))

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

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.

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.
    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.
    a = 10; b = 20; print(a, b) # Output: 10 20
    
    value = 5
    if value > 3: print("Greater"); print("Than 3") # Less clear
    
    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).

Navigation