Skip to content

Getting Started

Installation

Terminal window
npm install python2ts
# or
pnpm add python2ts
# or
yarn add python2ts

CLI Usage

Transpile a Python file to TypeScript:

Terminal window
# Transpile to stdout
npx python2ts input.py
# Transpile to file
npx python2ts input.py -o output.ts
# Pipe from stdin
cat script.py | npx python2ts > script.ts
# Without runtime import (for inspection)
npx python2ts input.py --no-runtime

Programmatic API

import { transpile } from "python2ts"
const typescript = transpile(`
def greet(name: str) -> str:
return f"Hello, {name}!"
`)
console.log(typescript)
// Output:
// function greet(name: string): string {
// return `Hello, ${name}!`
// }

Advanced API

For more control, use the step-by-step API:

import { parse, transform, generate } from "python2ts"
// Step 1: Parse Python to AST
const ast = parse(pythonCode)
// Step 2: Transform to TypeScript AST
const transformed = transform(ast)
// Step 3: Generate TypeScript code
const { code, usedRuntimeFunctions } = generate(transformed, {
includeRuntime: true,
runtimeImportPath: "pythonlib"
})

Using pythonlib Standalone

Don’t need the transpiler? Use pythonlib directly for Python-style utilities:

Terminal window
npm install pythonlib
// Builtins from main export
import { range, enumerate, sorted, len, zip } from "pythonlib"
// Module functions from subpaths
import { combinations, permutations } from "pythonlib/itertools"
import { Counter, defaultdict, deque } from "pythonlib/collections"
import { lruCache, partial, pipe } from "pythonlib/functools"
import { Path } from "pythonlib/pathlib"
import { sha256 } from "pythonlib/hashlib"

All Available Modules

pythonlib provides 20+ modules covering Python’s most useful standard library functionality:

Core Utilities

Import PathKey Functions
pythonlibrange, enumerate, zip, sorted, len, min, max, sum
pythonlib/itertoolscombinations, permutations, product, chain, cycle, groupby, takeWhile, dropWhile
pythonlib/functoolslruCache, partial, reduce, pipe, compose, cache
pythonlib/collectionsCounter, defaultdict, deque, OrderedDict

Data Processing

Import PathKey Functions
pythonlib/datetimedatetime, date, time, timedelta
pythonlib/jsonloads, dumps, load, dump
pythonlib/research, match, findAll, sub, compile
pythonlib/stringTemplate, capWords, asciiLowercase, digits

Math & Random

Import PathKey Functions
pythonlib/mathsqrt, floor, ceil, factorial, gcd, lcm, pi, e
pythonlib/randomrandInt, choice, shuffle, sample, uniform, random

File System (Node.js/Deno/Bun)

Import PathKey Functions
pythonlib/ospath.join, path.dirname, environ, getcwd, listdir, walk
pythonlib/pathlibPath class with readText, writeText, exists, glob
pythonlib/globglob, iglob, pattern matching
pythonlib/shutilcopy, copy2, move, rmtree, which
pythonlib/tempfileNamedTemporaryFile, TemporaryDirectory, mkdtemp

Security & Encoding

Import PathKey Functions
pythonlib/hashlibmd5, sha1, sha256, sha512, newHash
pythonlib/base64b64encode, b64decode, urlsafeB64encode
pythonlib/uuiduuid4, uuid1, UUID class

System & Network

Import PathKey Functions
pythonlib/sysargv, platform, exit, version
pythonlib/subprocessrun, call, checkCall, checkOutput
pythonlib/urlliburlparse, urljoin, urlencode, quote
pythonlib/timetime, sleep, strftime, localtime
pythonlib/logginggetLogger, basicConfig, debug, info, error
pythonlib/copycopy, deepcopy

Quick Examples

itertools

import { combinations, product, chain } from "pythonlib/itertools"
// All 2-combinations of [1, 2, 3]
for (const combo of combinations([1, 2, 3], 2)) {
console.log(combo) // [1,2], [1,3], [2,3]
}
// Cartesian product
[...product(["a", "b"], [1, 2])]
// [["a", 1], ["a", 2], ["b", 1], ["b", 2]]
// Chain iterables
[...chain([1, 2], [3, 4], [5])]
// [1, 2, 3, 4, 5]

collections

import { Counter, defaultdict, deque } from "pythonlib/collections"
// Count occurrences
const counter = new Counter("mississippi")
counter.mostCommon(2) // [["i", 4], ["s", 4]]
// Default values for missing keys
const graph = defaultdict<string, string[]>(() => [])
graph.get("node1").push("node2")
graph.get("node1").push("node3")
// Double-ended queue
const dq = new deque([1, 2, 3])
dq.appendLeft(0) // O(1) prepend
dq.rotate(1) // Rotate right

functools

import { lruCache, partial, pipe } from "pythonlib/functools"
// Memoization
const fib = lruCache((n: number): number => (n <= 1 ? n : fib(n - 1) + fib(n - 2)))
fib(100) // Instant, even for large values
// Partial application
const add = (a: number, b: number) => a + b
const add10 = partial(add, 10)
add10(5) // 15
// Function composition
const process = pipe(
(x: number) => x * 2,
(x: number) => x + 1,
(x: number) => x.toString()
)
process(5) // "11"

pathlib

import { Path } from "pythonlib/pathlib"
const p = new Path("/home/user/documents/file.txt")
p.name // "file.txt"
p.stem // "file"
p.suffix // ".txt"
p.parent // Path("/home/user/documents")
// File operations (async)
await p.exists()
await p.readText()
await p.writeText("Hello World")
// Glob patterns
const pyFiles = await p.parent.glob("*.py")

hashlib

import { sha256, md5 } from "pythonlib/hashlib"
const hash = sha256()
hash.update("Hello ")
hash.update("World")
hash.hexdigest()
// "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"
// One-liner
sha256("password").hexdigest()

datetime

import { datetime, timedelta } from "pythonlib/datetime"
const now = datetime.now()
const nextWeek = now.add(timedelta({ days: 7 }))
now.strftime("%Y-%m-%d %H:%M:%S")
// "2024-01-15 14:30:00"
nextWeek.isoformat()
// "2024-01-22T14:30:00"

Next Steps