Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
default
✓ import python2ts from 'python2ts'
✗ const python2ts = require('python2ts')
python2ts is ESM-only starting from v1.0; CommonJS require will throw an error.
transpile
✓ import { transpile } from 'python2ts'
✗ import transpile from 'python2ts'
transpile is a named export, not a default export.
TranspileOptions
✓ import type { TranspileOptions } from 'python2ts/types'
✗ import { TranspileOptions } from 'python2ts'
Type imports should be from the 'types' subpath to avoid runtime overhead.
CLI
✓ import { cli } from 'python2ts'
✗ import * as python2ts from 'python2ts'; python2ts.cli()
CLI is exported as a named function; namespace import works but direct named import is preferred.
Shows basic transpilation of a Python function with type hints and f-string to TypeScript.
import { transpile } from 'python2ts';
const pythonCode = `
def greet(name: str) -> str:
return f"Hello, {name}!"
`;
try {
const result = transpile(pythonCode, {
filename: 'example.py',
runtime: 'pythonlib', // default runtime path
});
console.log(result.code);
// Output:
// import { greet } from "pythonlib/builtins"
// function greet(name: string): string {
// return `Hello, ${name}!`;
// }
} catch (error) {
console.error('Transpilation failed:', error.message);
}
python2ts --version
Debug
Known issues
gotchaThe TranspileOptions type is not exported directly from 'python2ts'; import from 'python2ts/types' instead.fiximport type { TranspileOptions } from 'python2ts/types'; affects: >=1.0
deprecatedUsing '--target es5' in CLI options is deprecated; ES2015+ is required.fixRemove --target flag or use a modern target like es2020.
affects: >=1.4
gotchaTranspiled code depends on the 'pythonlib' package at runtime; it is not a standalone output.fixEnsure 'pythonlib' is installed in your project: npm install pythonlib
affects: >=1.0
breakingIn pythonlib v2.0.0, hashlib functions digest(), hexdigest(), pbkdf2Hmac(), scrypt(), and compareDigest() now return Promises and must be awaited.fixUpdate transpiled code to await these calls, e.g., const hash = await digest(data);
affects: >=2.0.0 (pythonlib)
gotchaCLI output defaults to stdout; using -o with an existing file will overwrite without confirmation.fixEnsure the output file path is correct or that you have a backup.
affects: >=1.0
breakingVersion 1.0 dropped CommonJS support; the package is ESM-only.fixUse ES module imports (import/export) in your project. Update tsconfig to include 'module': 'ESNext'.
affects: >=1.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/python2ts/index.js not supported.
Python2ts is ESM-only; using require() in a CommonJS context fails.
fixSwitch your project to ES modules or use dynamic import(): const python2ts = await import('python2ts'); TypeError: python2ts.transpile is not a function
Incorrect import: likely using default import instead of named import.
fixUse import { transpile } from 'python2ts'; Cannot find module 'pythonlib/builtins' or similar import in transpiled output.
The 'pythonlib' runtime package is not installed.
fixRun npm install pythonlib in the target project.
SyntaxError: Unexpected token 'export' at ...
The transpiled output uses ES module syntax but is loaded in a CommonJS context.
fixEnsure the transpiled file is loaded as an ES module (e.g., use .mjs extension or { 'type': 'module' } in package.json). Audit
Dependencies
pythonlibrequiredProvides runtime implementations for Python standard library modules (e.g., collections, itertools, hashlib, os) used in transpiled code.