Skip to main content

A simple and fast Python utility to remove comments from Python scripts.

Project description

PyCommentCleaner

A simple and fast Python utility to remove comments from Python scripts.

Features

  • Removes inline and standalone comments from Python files
  • Preserves comments inside strings and docstrings
  • Provides both command-line interface and Python API
  • Maintains original formatting and whitespace
  • Handles complex Python syntax elements correctly

Installation

pip install pycommentcleaner

Usage

Command Line Interface

# Clean a single file
pycommentcleaner path/to/your_script.py

# Clean multiple files
pycommentcleaner path/to/file1.py path/to/file2.py

# Specify an output directory
pycommentcleaner path/to/file.py --output-dir path/to/output

# Increase verbosity
pycommentcleaner path/to/file.py -v     # Warning level
pycommentcleaner path/to/file.py -vv    # Info level
pycommentcleaner path/to/file.py -vvv   # Debug level

# Show help
pycommentcleaner --help

Python API

# Clean a file
from pycommentcleaner import clean_file

success, message = clean_file("path/to/your_script.py")
print(message)

# Clean code directly
from pycommentcleaner import clean_code

code = """
def hello():
    # This is a comment
    print("Hello, world!")  # This is another comment
    
    # This comment will be removed
    string_with_hash = "This # is not a comment"
"""

cleaned_code = clean_code(code)
print(cleaned_code)

Examples

Before:

# This is a comment at the beginning of the file
def hello():
    # This is a function that prints a greeting
    print("Hello, world!")  # This prints a greeting
    
    string_with_hash = "This # is not a comment"
    """This is a docstring
    # This is not a comment because it's in a docstring
    """

After:

 
def hello():
     
    print("Hello, world!")   
    
    string_with_hash = "This # is not a comment"
    """This is a docstring
    # This is not a comment because it's in a docstring
    """

Development

Setup

  1. Clone the repository:

    git clone https://github.com/viekayy/pycommentcleaner.git
    cd example
    
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install development dependencies:

    pip install -e ".[dev]"
    

Running Tests

pytest

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycommentcleaner-0.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycommentcleaner-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pycommentcleaner-0.1.1.tar.gz.

File metadata

  • Download URL: pycommentcleaner-0.1.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pycommentcleaner-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d1f917090b7846fdb6259a92ac91650b446dd6665e5a4b0aa5e6624df319cf9a
MD5 5607453e1b0d791b9139ed4f840c92e8
BLAKE2b-256 b223c93eeeef132494dbb6d95dd237f140cce3d5b0ddd0c0e304e57b13c02a45

See more details on using hashes here.

File details

Details for the file pycommentcleaner-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pycommentcleaner-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe90556b3d34b6d4e12fc69299aa543c0799d884c13e5eb7138affcd4e82f210
MD5 4671d3d4a8c563c7c787fdd13f7a5805
BLAKE2b-256 17fe3f3e67423f4cb31beb6e527d9fc1fc489aac71107fb69605a3160c448f4a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page