Registry / serialization / comment-parser

comment-parser

JSON →
library1.2.5pypypi✓ verified 86d ago

comment-parser is a Python module designed to extract comments from various source code files. It supports common languages like C, C++, Java, JavaScript, Python, and others, handling both single-line and multi-line comment formats. The library, currently at version 1.2.5, features an active release schedule, with recent updates focusing on setup stability and adding typing information.

pip install comment-parser
INSTALL
IMPORT
SIG · COMMENT-PARSER
C
comment-parser
serializationpythonv1.2.5
Install
2.2s avg
Import
66ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.066s · 19.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.2s · import 0.067s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

comment_parser
from comment_parser import comment_parser
The main entry point for the library's parsing functions.
Comment
from comment_parser.parsers.common import Comment
The object type returned by parsing functions, representing an extracted comment. Often accessed directly from the list returned by `extract_comments`.

This quickstart demonstrates how to extract comments from both a string and a temporary file. It uses `extract_comments_from_str` and `extract_comments` by explicitly specifying the MIME type for the code, which is recommended for reliable parsing across different languages. The output for each comment includes its text, line number, and whether it's a multi-line comment.

import os import tempfile from comment_parser import comment_parser # Example 1: Extract comments from a string python_code = """ # This is a single-line Python comment def example_function(): ''' This is a docstring, not a comment parsed by comment-parser by default. ''' x = 10 # Inline comment # Another line """ try: # Explicitly specify MIME type for robust parsing comments_from_str = comment_parser.extract_comments_from_str(python_code, mime='text/x-python') print("\n--- Comments from string ---") for comment in comments_from_str: print(f"[Line {comment.line_number}] {comment.text} (Multiline: {comment.is_multiline})") except Exception as e: print(f"Error parsing string: {e}") # Example 2: Extract comments from a file # Create a dummy file for demonstration temp_dir = tempfile.gettempdir() file_path = os.path.join(temp_dir, 'example.c') c_code = """ /* This is a multi-line C comment * spanning several lines. */ #include <stdio.h> // Single line C comment int main() { printf("Hello, World!"); return 0; } """ with open(file_path, 'w') as f: f.write(c_code) try: # Using extract_comments with a file path comments_from_file = comment_parser.extract_comments(file_path, mime='text/x-c') print("\n--- Comments from file ---") for comment in comments_from_file: print(f"[Line {comment.line_number}] {comment.text} (Multiline: {comment.is_multiline})") except Exception as e: print(f"Error parsing file: {e}") finally: # Clean up the dummy file if os.path.exists(file_path): os.remove(file_path)
Debug
Known issues
gotchaAutomatic MIME type detection requires external dependencies (`python-magic` for Python and `libmagic` system library). Without these, the parser might fail to correctly identify file types, leading to `UnsupportedError`.
fix
On OSX/Windows, `pip install python-magic`. On Linux/Unix, install `libmagic` via your system package manager (e.g., `sudo apt-get install libmagic-dev` or `sudo yum install file-devel`) in addition to `pip install comment-parser`. Consider explicitly passing the `mime` argument to `extract_comments` or `extract_comments_from_str` for unsupported types or when autodetection fails.
affects: All versions
gotchaThe library raises an `UnsupportedError` if it encounters a file or string with a MIME type that it does not have a parser for, or if MIME type deduction fails. While many common languages are supported (C, C++, Java, Python, JavaScript, HTML, XML, Go, Ruby, Shell), niche or custom language files may not be.
fix
Always check the return value or handle the `UnsupportedError` exception. If the MIME type is known, pass it explicitly using the `mime` argument (e.g., `mime='text/x-python'`). Refer to the project's `comment_parser.py` source for the definitive list of supported MIME types.
affects: All versions
gotchaThis Python library `comment-parser` is distinct from a similarly named JavaScript library (`comment-parser` on NPM) and a Rust crate (`comment-parser` on crates.io). Features, APIs, and breaking changes from those other ecosystems do not apply here.
fix
Ensure you are using `pip install comment-parser` and importing `from comment_parser import comment_parser` to use the Python version of the library.
affects: All versions
Errors
Common errors & fixes
comment_parser.errors.UnsupportedError: The given mime type for the file is not supported.
The library does not have a parser for the detected or provided MIME type, or `python-magic` (and `libmagic`) is not installed/configured to correctly deduce the MIME type.
fix
Explicitly provide the `mime` argument (e.g., `comment_parser.extract_comments(filename, mime='text/x-python')`). If the language is genuinely unsupported, consider using a different tool or contributing a parser. Ensure `python-magic` and `libmagic` (for Linux/Unix) are installed if relying on auto-detection.
ModuleNotFoundError: No module named 'magic'
The `python-magic` library, which `comment-parser` uses for automatic MIME type deduction on some operating systems, is not installed.
fix
Install the `python-magic` package: `pip install python-magic`. On Linux/Unix, also ensure the `libmagic` system library is installed (e.g., `sudo apt-get install libmagic-dev` or `sudo yum install file-devel`).
Upgrade
Version history
1.2.5latest on PyPI · released Mar 16, 2026
Audit
Dependencies
python-magicoptionalRequired for automatic MIME type deduction on OSX and Windows. Linux/Unix systems may require the 'libmagic' system library.
Agent activity
8 hits · last 30 days
node
6
Amazon
1
OpenAI (training)
1
Resources
comment-parser — pip install comment-parser · libregistry