Skip to content

print (T201)

Derived from the flake8-print linter.

Fix is sometimes available.

What it does

Checks for print statements.

Why is this bad?

print statements used for debugging should be omitted from production code. They can lead the accidental inclusion of sensitive information in logs, and are not configurable by clients, unlike logging statements.

print statements used to produce output as a part of a command-line interface program are not typically a problem.

Example

def sum_less_than_four(a, b):
    print(f"Calling sum_less_than_four")
    return a + b < 4

The automatic fix will remove the print statement entirely:

def sum_less_than_four(a, b):
    return a + b < 4

To keep the line for logging purposes, instead use something like:

import logging

logging.basicConfig(level=logging.INFO)


def sum_less_than_four(a, b):
    logging.debug("Calling sum_less_than_four")
    return a + b < 4

Fix safety

This rule's fix is marked as unsafe, as it will remove print statements that are used beyond debugging purposes.