Node.js library that enables seamless integration of Python libraries into JavaScript applications
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for importing and using Python modules with full access to their APIs and transparent JavaScript integration.
Import any Python module by name, returning a PyObject wrapper that provides access to all module attributes and functions.
/**
* Import a Python module by name
* @param name - Python module name (e.g., 'numpy', 'pandas', 'sys')
* @returns PyObject wrapper for the imported module
*/
function pymport(name: string): PyObject;Usage Examples:
import { pymport } from 'pymport';
// Import standard library modules
const sys = pymport('sys');
const os = pymport('os');
// Import third-party packages (after installing via pympip)
const numpy = pymport('numpy');
const pandas = pymport('pandas');
// Access module attributes
console.log(sys.get('version').toString());
console.log(os.get('name').toString());
// Call module functions
const array = numpy.get('array').call([1, 2, 3, 4, 5]);Create a proxified version of a PyObject that behaves like a native JavaScript object, enabling natural Python-like syntax.
/**
* Create a proxified version of a PyObject for natural JavaScript interaction
* @param v - PyObject to proxify
* @param name - Optional name for proxified function objects
* @returns Proxified object that behaves like native JavaScript
*/
function proxify(v: PyObject, name?: string): any;Usage Examples:
import { pymport, proxify } from 'pymport';
// Standard PyObject usage (verbose)
const numpy = pymport('numpy');
const array = numpy.get('array').call([1, 2, 3]);
const reshaped = array.get('reshape').call(3, 1);
// Proxified usage (natural Python-like syntax)
const np = proxify(pymport('numpy'));
const array2 = np.array([1, 2, 3]);
const reshaped2 = array2.reshape(3, 1);
// Proxified functions work with method chaining
const result = np.arange(12).reshape(3, 4).sum();
// Access properties naturally
console.log(np.pi); // π constant
console.log(np.__version__); // numpy versionExecute Python expressions with optional global and local contexts, useful for dynamic Python code execution.
/**
* Evaluate a Python expression and return the result
* @param code - Python code string (must be an expression, not statements)
* @param globals - Optional global context for execution
* @param locals - Optional local context for execution
* @returns PyObject result of the evaluation
*/
function pyval(
code: string,
globals?: PyObject | Record<string, any>,
locals?: PyObject | Record<string, any>
): PyObject;Usage Examples:
import { pyval, pymport } from 'pymport';
// Simple expression evaluation
const result = pyval('2 + 3 * 4');
console.log(result.toJS()); // 14
// Using imported modules in evaluation
const np = pymport('numpy');
const array = pyval('np.array([1, 2, 3]) * 2', { np });
console.log(array.toJS()); // [2, 4, 6]
// Complex expressions with variables
const globals = { x: 10, y: 20 };
const result2 = pyval('x ** 2 + y ** 2', globals);
console.log(result2.toJS()); // 500Python module search paths can be configured before importing pymport:
// Set Python path to include local modules
process.env['PYTHONPATH'] = __dirname;
// Import pymport after setting environment
const { pymport } = require('pymport');
// Now can import local Python files
const myModule = pymport('my_local_module');pymport supports all Python modules that are compatible with the built-in Python interpreter:
sys, os, math, json, urllib, etc.numpy, scipy, pandas (install via pympip)matplotlib, seaborn (install via pympip)scikit-learn, tensorflow (install via pympip)Use the included pympip tool to install Python packages:
# Install packages for use with pymport
npx pympip install numpy pandas matplotlib
npx pympip3 install scikit-learn tensorflow
# List installed packages
npx pympip listInstall with Tessl CLI
npx tessl i tessl/npm-pymport