CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pymport

Node.js library that enables seamless integration of Python libraries into JavaScript applications

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

python-modules.mddocs/

Python Module Import

Core functionality for importing and using Python modules with full access to their APIs and transparent JavaScript integration.

Capabilities

Module Import Function

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]);

Proxification Function

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 version

Python Code Evaluation

Execute 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()); // 500

Module Path Configuration

Python 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');

Supported Python Modules

pymport supports all Python modules that are compatible with the built-in Python interpreter:

  • Standard Library: sys, os, math, json, urllib, etc.
  • Scientific Computing: numpy, scipy, pandas (install via pympip)
  • Data Visualization: matplotlib, seaborn (install via pympip)
  • Machine Learning: scikit-learn, tensorflow (install via pympip)
  • Custom Modules: Any Python files in the PYTHONPATH

Installation of Python Packages

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 list

Install with Tessl CLI

npx tessl i tessl/npm-pymport

docs

error-handling.md

index.md

object-creation.md

pyobject.md

python-modules.md

type-conversion.md

tile.json