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
-
Clone the repository:
git clone https://github.com/viekayy/pycommentcleaner.git cd example
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
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
- Viadishwar - GitHub
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
422bf2918b788b68fea27a2d07a4dee2e6469fd571384e7355a8d1789fc88fa7
|
|
| MD5 |
ded79b1eda0b2bcadf25914072121f23
|
|
| BLAKE2b-256 |
43f9ff9fc503a6f2643f1b86a6fead783e52d44a957e3cac394c5691036f4705
|
File details
Details for the file pycommentcleaner-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pycommentcleaner-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15180d1822a7a18ab3466aecf1bb79a3039a5c52a0164c917261077e9b13aa06
|
|
| MD5 |
0ff89bcfd2193b876f1e3388afe390aa
|
|
| BLAKE2b-256 |
bea5b22814739b2ecbbabb95b6d5cd547b4adf5e35c86ba497e36be9b98afe13
|