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.2.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.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pycommentcleaner-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 422bf2918b788b68fea27a2d07a4dee2e6469fd571384e7355a8d1789fc88fa7
MD5 ded79b1eda0b2bcadf25914072121f23
BLAKE2b-256 43f9ff9fc503a6f2643f1b86a6fead783e52d44a957e3cac394c5691036f4705

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycommentcleaner-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 15180d1822a7a18ab3466aecf1bb79a3039a5c52a0164c917261077e9b13aa06
MD5 0ff89bcfd2193b876f1e3388afe390aa
BLAKE2b-256 bea5b22814739b2ecbbabb95b6d5cd547b4adf5e35c86ba497e36be9b98afe13

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