{"id":26136,"library":"python2ts","title":"python2ts","description":"python2ts is an AST-based Python-to-TypeScript transpiler that converts Python code into clean, idiomatic TypeScript while preserving type annotations and semantics. The current stable version is 1.4.3 (January 2026), with active development and frequent releases (multiple per week). It supports a wide range of Python features including dataclasses, list/dict comprehensions, pattern matching, f-strings, async/await, decorators, context managers, generators, and standard library modules via the accompanying pythonlib runtime. Unlike other transpilers, python2ts produces readable, maintainable TypeScript output with full type preservation and has minimal runtime overhead, making it suitable for migrating large Python codebases to TypeScript. Requires Node >=22.0.0.","status":"active","version":"1.4.3","language":"javascript","source_language":"en","source_url":"https://github.com/sebastian-software/python2ts","tags":["javascript","python","typescript","transpiler","compiler","ast","converter","code-generation","type-inference"],"install":[{"cmd":"npm install python2ts","lang":"bash","label":"npm"},{"cmd":"yarn add python2ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add python2ts","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides runtime implementations for Python standard library modules (e.g., collections, itertools, hashlib, os) used in transpiled code.","package":"pythonlib","optional":false}],"imports":[{"note":"python2ts is ESM-only starting from v1.0; CommonJS require will throw an error.","wrong":"const python2ts = require('python2ts')","symbol":"default","correct":"import python2ts from 'python2ts'"},{"note":"transpile is a named export, not a default export.","wrong":"import transpile from 'python2ts'","symbol":"transpile","correct":"import { transpile } from 'python2ts'"},{"note":"Type imports should be from the 'types' subpath to avoid runtime overhead.","wrong":"import { TranspileOptions } from 'python2ts'","symbol":"TranspileOptions","correct":"import type { TranspileOptions } from 'python2ts/types'"},{"note":"CLI is exported as a named function; namespace import works but direct named import is preferred.","wrong":"import * as python2ts from 'python2ts'; python2ts.cli()","symbol":"CLI","correct":"import { cli } from 'python2ts'"}],"quickstart":{"code":"import { transpile } from 'python2ts';\n\nconst pythonCode = `\ndef greet(name: str) -> str:\n    return f\"Hello, {name}!\"\n`;\n\ntry {\n  const result = transpile(pythonCode, {\n    filename: 'example.py',\n    runtime: 'pythonlib', // default runtime path\n  });\n  console.log(result.code);\n  // Output:\n  // import { greet } from \"pythonlib/builtins\"\n  // function greet(name: string): string {\n  //   return `Hello, ${name}!`;\n  // }\n} catch (error) {\n  console.error('Transpilation failed:', error.message);\n}","lang":"typescript","description":"Shows basic transpilation of a Python function with type hints and f-string to TypeScript."},"warnings":[{"fix":"import type { TranspileOptions } from 'python2ts/types';","message":"The TranspileOptions type is not exported directly from 'python2ts'; import from 'python2ts/types' instead.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Remove --target flag or use a modern target like es2020.","message":"Using '--target es5' in CLI options is deprecated; ES2015+ is required.","severity":"deprecated","affected_versions":">=1.4"},{"fix":"Ensure 'pythonlib' is installed in your project: npm install pythonlib","message":"Transpiled code depends on the 'pythonlib' package at runtime; it is not a standalone output.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Update transpiled code to await these calls, e.g., const hash = await digest(data);","message":"In pythonlib v2.0.0, hashlib functions digest(), hexdigest(), pbkdf2Hmac(), scrypt(), and compareDigest() now return Promises and must be awaited.","severity":"breaking","affected_versions":">=2.0.0 (pythonlib)"},{"fix":"Ensure the output file path is correct or that you have a backup.","message":"CLI output defaults to stdout; using -o with an existing file will overwrite without confirmation.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Use ES module imports (import/export) in your project. Update tsconfig to include 'module': 'ESNext'.","message":"Version 1.0 dropped CommonJS support; the package is ESM-only.","severity":"breaking","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Switch your project to ES modules or use dynamic import(): const python2ts = await import('python2ts');","cause":"Python2ts is ESM-only; using require() in a CommonJS context fails.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/python2ts/index.js not supported."},{"fix":"Use import { transpile } from 'python2ts';","cause":"Incorrect import: likely using default import instead of named import.","error":"TypeError: python2ts.transpile is not a function"},{"fix":"Run npm install pythonlib in the target project.","cause":"The 'pythonlib' runtime package is not installed.","error":"Cannot find module 'pythonlib/builtins' or similar import in transpiled output."},{"fix":"Ensure the transpiled file is loaded as an ES module (e.g., use .mjs extension or { 'type': 'module' } in package.json).","cause":"The transpiled output uses ES module syntax but is loaded in a CommonJS context.","error":"SyntaxError: Unexpected token 'export' at ..."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}