Skip to content

How to Fix Python SyntaxError: Missing Parentheses in Call to 'print'

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix Python SyntaxError: Missing Parentheses in Call to 'print. We cover key concepts, practical examples, and best practices.

The Problem

You see SyntaxError: Missing parentheses in call to 'print' when running a script that uses Python 2's print "hello" syntax under Python 3, which requires print("hello").

Quick Fix

Step 1: Identify all print statements

Run the script to see the first offending line:

python3 script.py
  File "script.py", line 5
    print "Hello, World!"
          ^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Step 2: Add parentheses to every print

Change Python 2 style to Python 3 style:

# Before (Python 2)
print "Hello"
print "Value:", x
print >> sys.stderr, "Error"

# After (Python 3)
print("Hello")
print("Value:", x)
print("Error", file=sys.stderr)

Step 3: Use 2to3 for automatic conversion

Run Python's built-in 2to3 tool to fix the entire file:

2to3 -w script.py
RefactoringTool: Refactored script.py
--- script.py (original)
+++ script.py (refactored)
@@ -1,2 +1,2 @@
-print "Hello"
+print("Hello")

The -w flag writes changes directly to the file.

Step 4: Check for other Python 2 syntax

Use 2to3 to identify other Python 2 patterns:

2to3 script.py

This reports all Python 2 syntax including raw_input(), urllib, and except Exception, e without modifying the file.

Alternative Solutions

Use from future import print_function

In a Python 2 codebase, add this import to use Python 3 print syntax without migrating fully:

from __future__ import print_function
print('Hello')  # Now works in both Python 2 and 3

Run Python with the -3 flag

The -3 flag warns about Python 2 syntax that will not work in Python 3:

python -3 script.py

Common Mistakes to Avoid

Running a Python 2 script with python3. Scripts written for Python 2 use print as a statement. Always check the shebang line or specify the interpreter explicitly.

Forgetting to parenthesize print in a function call. print "value", x works in Python 2 but fails in Python 3. Use print("value", x).

Assuming the system python is Python 3. On many systems, python points to Python 2 and python3 points to Python 3. Use python3 explicitly.

Pro Tips

Use black's --check flag in CI. Run black --check . to automatically find formatting and syntax issues without modifying files.

Use a shebang line to enforce the interpreter. Add #!/usr/bin/env python3 at the top of every script to ensure it runs with Python 3.

Run python with -Wall for warnings. The python3 -Wall script.py flag shows deprecation warnings for Python 2 patterns, helping you catch them early.

Check the shebang line on every script. Use head -1 script.py to verify the interpreter is python3, not python, to avoid accidental Python 2 execution.

Prevention

  • Use python3 explicitly instead of python to avoid accidentally using Python 2.
  • Add a shebang line #!/usr/bin/env python3 at the top of scripts.
  • Run 2to3 on legacy codebases before migrating to Python 3.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro