Install & Compatibility
Where this runs
tested against v8.694.25301 · 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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.181s · 18.6MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.8s · import 0.168s · 19MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Word
✓ from mo_parsing import Word
Literal
✓ from mo_parsing import Literal
Keyword
✓ from mo_parsing import Keyword
Group
✓ from mo_parsing import Group
Regex
✓ from mo_parsing import Regex
alphas
✓ from mo_parsing.utils import alphas
Whitespace
✓ from mo_parsing.whitespaces import Whitespace
✗ from mo_parsing import Whitespace
Whitespace context manager is in its own submodule.
This quickstart demonstrates defining a simple grammar using `Word` and `Literal`, managing whitespace with `Whitespace` context, and parsing a string. It shows how to access the parsed results as both a list and a dictionary.
from mo_parsing import Word, Literal
from mo_parsing.utils import alphas
from mo_parsing.whitespaces import Whitespace
# Define a simple grammar for 'Hello, World!' with a custom whitespace rule
with Whitespace(' \t') as whitespace_context:
greet = Word(alphas)('greeting') + Literal(',') + Word(alphas)('person') + Literal('!')
# Parse a string
result = greet.parse_string('Hello, World!')
# Access results as a list
print(f"List view: {list(result)}")
# Access results as a dictionary (for named tokens)
print(f"Dict view: {dict(result)}")
# Example with different input
result_named = greet.parse_string('Hi, There!')
print(f"Named tokens: {result_named.greeting}, {result_named.person}")
Debug
Known issues
breakingParserElements are static. Methods like `.add_parse_action(action)` create and return a *new* ParserElement. If the result is not assigned back to a variable, the parse action (or any other modification) will be lost.fixAlways reassign the result of methods that modify ParserElements: `my_parser = my_parser.add_parse_action(my_action)`
affects: All versions (since fork from pyparsing)
breakingWhitespace handling has been refactored. `mo-parsing` uses the `mo_parsing.whitespaces.Whitespace` context manager to define ignored characters, replacing `pyparsing`'s global `whitespace_chars` or `parser.set_whitespace_chars()` methods, which are removed.fixUse the `with Whitespace(...) as context:` block or `Whitespace().use()` for language definition.
affects: All versions (since fork from pyparsing)
gotchaThe parameter order for parse actions is `(tokens, index, string)`, which is the opposite of `pyparsing`'s `(string, location, tokens)`.fixAdjust your parse action signatures to `def my_action(tokens, loc, string):` or `lambda t, l, s: ...`
affects: All versions (since fork from pyparsing)
gotchaDo not confuse `Word()`, `Literal()`, and `Keyword()` for exact string matching. `Word(string_of_characters)` matches any contiguous sequence of *characters found within* `string_of_characters`, not the exact `string_of_characters` itself.fixUse `Literal('exact string')` for a fixed string match, or `Keyword('exact word')` for a fixed string that must be a whole word (with word boundaries). affects: All versions (inherited from pyparsing behavior)
Errors
Common errors & fixes
TypeError: 'NoneType' object is not callable (when a parse action doesn't seem to run)
You likely applied a parse action to a ParserElement without reassigning the result, e.g., `some_parser.add_parse_action(my_func)` instead of `some_parser = some_parser.add_parse_action(my_func)`.
fixRemember that `ParserElement` methods like `add_parse_action` return a *new* ParserElement object. Always assign the result back to the variable: `my_parser = my_parser.add_parse_action(my_func)`.
mo_parsing.exceptions.ParseException: Expected 'something_else' (when parser consumes too much or wrong input)
A common cause is misusing `Word(chars)` where `Literal(exact_string)` or `Keyword(exact_word)` was intended. `Word('abc')` will match 'apple', 'banana', 'cat' because 'a', 'b', 'c' are in its character set, not just 'abc'.
fixIf you need to match a specific string exactly, use `Literal('your_string')`. If it must be a whole word, use `Keyword('your_word')`. mo_parsing.exceptions.ParseException: Expected ... (when trying to match a literal phrase with spaces)
Using `Literal('multiple words')` will attempt to match the entire string literally, including spaces, without allowing for intervening whitespace skipping behavior.
fixFor phrases with spaces that should allow for arbitrary whitespace (and comments if configured), split them into separate `Literal` or `Keyword` elements: `Literal('first') + Literal('second')`. Upgrade
Version history
8.694.25301latest on PyPI · released Oct 28, 2025
Audit
Dependencies
No dependency data recorded yet.