Registry / serialization / pyjsparser

pyjsparser

JSON →
library2.7.1pypypi✓ verified 24d ago

Pyjsparser is a fast JavaScript parser for Python, based on a manual translation of esprima.js. It aims to be a comprehensible JavaScript parser for Python, supporting ECMAScript 5.1 and parts of ECMAScript 6. The library is currently at version 2.7.1, with its last release dating back to April 2019, suggesting a maintenance-level release cadence rather than active development.

pip install pyjsparser
INSTALL
IMPORT
SIG · PYJSPARSER
P
pyjsparser
serializationpythonv2.7.1
Install
2.4s avg
Import
44ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.7.1 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.038s · 19.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.032s · 20MB
17MB installed
● package 17MB
Code
Verified usage

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

parse
from pyjsparser import parse

Parses a given JavaScript string into an Abstract Syntax Tree (AST).

from pyjsparser import parse js_code = 'var message = "Hello, Pyjsparser!"; console.log(message);' ast = parse(js_code) print(ast) # Example of accessing a property (structure depends on AST) # For illustration, this may need adjustment based on the exact AST output # if ast and ast['type'] == 'Program' and ast['body']: # first_statement = ast['body'][0] # if first_statement['type'] == 'VariableDeclaration': # print(f"Variable declared: {first_statement['declarations'][0]['id']['name']}")
Debug
Known issues
gotchaThe library's last release was in April 2019. While functional, it has not received updates for newer ECMAScript features beyond ES5.1 and parts of ES6. This means it may not correctly parse modern JavaScript syntax (ES2016+).
fix
For parsing modern JavaScript, consider alternative tools that are actively maintained or utilize more recent JavaScript engines.
affects: <=2.7.1
gotchaAlthough the `pyjsparser` wheel (version 2.7.1) mentions 'py2-none-any.whl', indicating Python 2 compatibility, the library also works with Python 3. However, due to the lack of recent updates, users might encounter compatibility issues or unexpected behavior with very recent Python 3 versions. Ensure thorough testing in your target Python environment.
fix
Test `pyjsparser` thoroughly with your specific Python 3 version. If issues arise, consider pinning an older, compatible Python 3 release or using an alternative parser for complex or modern use cases.
affects: All versions (due to age)
gotchaThe documentation states support for ECMAScript 5.1 and *parts* of ECMAScript 6. Users should not expect full compatibility with all features introduced in ES6 or any subsequent ECMAScript versions. Missing features might lead to parsing errors or an incomplete AST for modern JavaScript code.
fix
Review the official documentation (derived from esprima.js) for specific supported ECMAScript features if targeting anything beyond ES5.1.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyjsparser'
The `pyjsparser` library is not installed in your Python environment or the Python interpreter cannot find it in its search path.
fix
Install the library using pip: `pip install pyjsparser`
SyntaxError: invalid syntax
Pyjsparser primarily supports ECMAScript 5.1 and parts of ECMAScript 6. Attempting to parse modern JavaScript syntax (ES2016+ features like arrow functions, destructuring, spread syntax, etc.) will result in a syntax error because the parser does not understand these newer constructs.
fix
Ensure the JavaScript code you are parsing conforms to ECMAScript 5.1 or earlier ES6 features supported by pyjsparser. For modern JavaScript, consider using a more actively maintained parser.
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position X-Y: truncated \UXXXXXXXX escape
This error occurs when a string literal in your Python code, often a file path or JavaScript code, contains backslashes that Python interprets as incomplete or invalid Unicode escape sequences (e.g., `\U` followed by fewer than 8 hex digits) instead of literal backslashes.
fix
Use raw strings by prefixing the string literal with `r` (e.g., `r'C:\path\to\file.js'`) or escape the backslashes (e.g., `'C:\\path\\to\\file.js'`). Alternatively, use forward slashes for paths if the operating system supports it, as they do not trigger escape sequence interpretation.
pyjsparser.PyJsParserException: Expected
The JavaScript code contains syntax features not fully supported by `pyjsparser` (e.g., modern ES6+ features like `async`/`await`), or it is malformed. `pyjsparser` primarily supports ECMAScript 5.1 and limited parts of ES6.
fix
Ensure the JavaScript code adheres to ECMAScript 5.1 standards or features explicitly supported by `pyjsparser`. For modern JavaScript, consider using a more actively maintained parser.
ImportError: cannot import name 'parse' from 'pyjsparser'
The `parse` function is a top-level function within the `pyjsparser` module, not a separate name to be imported directly using `from pyjsparser import parse`.
fix
Access the `parse` function directly from the imported `pyjsparser` module: `import pyjsparser; ast = pyjsparser.parse("var a = 1;")`
Upgrade
Version history
2.7.1latest on PyPI · released Apr 21, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
pyjsparser — pip install pyjsparser · libregistry