Skip to main content

remove-print-statements

test codecov pre-commit.ci status PyPi Status Python versions MIT License

A command-line tool (and pre-commit hook) to remove all the print statements from your Python project.

Do you use print statements for debugging? We all do, and there's nothing wrong with it. After the bug has been resolved, we need to manually open all the files which we added the print statements in, only if we remember all of them after hours of debugging, and remove them. A better way would be to use some sort of find and replace from the editor or command-line, but that's still a lot of manual work. Worst case, it gets pushed and deployed to production.

Who wants to do all the manual work in the age of automation? No one. So, install this tool and forget about removing the print statements manually forever. You could either run this tool manually or add it as a pre-commit hook. You could even preview the print statements along with it's location without removing it. How nice is that!

Installation

You can install remove-print-statements from the Python Package Index (PyPI) with pipx or equivalent.

pipx install remove-print-statements

Or, try it out using pipx:

pipx run remove-print-statements --help

Or, with pre-commit in the repos section of your .pre-commit-config.yaml file (docs):

- repo: https://github.com/dhruvmanila/remove-print-statements
  rev: ''  # Replace with latest tag on GitHub
  hooks:
  - id: remove-print-statements
    # args: ['--verbose']   # Show all the print statements to be removed

Usage

Run it on a given set of files:

remove-print-statements foo.py bar.py ...
# or use globbing
remove-print-statements *.py

You can ignore files as well. To specify multiple files to ignore, use the flag multiple times otherwise it's difficult to know the difference between the files to ignore and the ones to check.

remove-print-statements *.py --ignore foo.py --ignore bar.py

You can preview the print statements which would be removed without modifying the source files using both --dry-run and --verbose flags like so:

$ remove-print-statements --dry-run --verbose test.py
test.py
  7 print("module")
  18 print("property")
  27 print("method")
  29 print("for loop")

1 file would be transformed, 4 print statements would be removed

remove-print-statements is a command-line tool that rewrites the files in place. It focuses on upgrading your code and not on making it look nice. Run remove-print-statements before formatters like Black.

remove-print-statements does not have any ability to recurse through directories. Use the pre-commit integration, globbing, or another technique for applying to many files such as with git ls-files | xargs.

Single statement

If there's only a single statement in the body of a function, for/while loop, if statement, etc., which is a print statement, then it will be replaced with the pass keyword.

def foo():
-    print()
+    pass


for _ in range(5):
-    print()
+    pass

if __name__ == "__main__":
-    print()
+    pass

Exit status

remove-print-statements command returns exit statuses as follows:

Status Description
0 No print statements / changes made successfully
1 Files would be updated (dry run)
123 Some error happened

Development

packaging: poetry code style: black pre-commit: enabled

Release

  1. Run poetry lock
  2. Bump version in pyproject.toml and __version__ variable
  3. Commit and push the changes with message release: <version>
  4. Publish a new release on GitHub which will trigger an automated workflow to publish on PyPi

License

remove-print-statements is licensed under the MIT License.

See LICENSE for details.

Metadata

Release files for remove-print-statements 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for remove-print-statements 0.5.2
File Size Uploaded
remove_print_statements-0.5.2.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for remove-print-statements 0.5.2
File Interpreter ABI Platform
remove_print_statements-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / remove_print_statements-0.5.2.tar.gz

Download URL remove_print_statements-0.5.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
61c7bac7feab96c712760c11285bc73f3caa822705bc192c67235cb297f22148
BLAKE2b-256 checksum
How to use checksums
b7123910d960b598cf623033c0a0a569fe43764dba7c66a26b957ed64088797b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.9.16 Linux/5.15.0-1038-azure

Release files / remove_print_statements-0.5.2-py3-none-any.whl

Download URL remove_print_statements-0.5.2-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d9e6f56b735cb32fe1d5a1bd07f158fedbc5a0cf2fc9b6bb3728fecefd99823
BLAKE2b-256 checksum
How to use checksums
e5e742bf9acbaa01df7d9738f00df1037a1eeba95bcaf845485ce4e8e28e7119
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.9.16 Linux/5.15.0-1038-azure

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page