Skip to content
Momtchil Momtchev edited this page Nov 21, 2022 · 22 revisions

pymport allows you to transparently use Python libraries from Node.js. It contains a fully self-contained embedded Python interpreter. It can also be built to use an existing external Python installation.

Two two interpreters share the same main thread and memory space. Python objects and V8 objects remain separate. V8 can access Python objects which have a PyObject type, while Python cannot access V8 objects. Python objects referenced from JavaScript must be freed by the V8 GC before being marked as available from the Python GC.

When passing objects between the interpreters:

  • JavaScript to Python conversion is automatic and by copying
  • Python objects are passed by reference to JavaScript as a PyObject
    • they can be automatically converted to JavaScript when the context permits it - as governed by the Symbol.toPrimitive() rules - for example in +a + 2 is a is a PyObject containing a number, it will be automatically converted to a JavaScript number
    • they can be explicitly converted to JavaScript by calling .toJS()
    • they can be directly interacted with it - for example if a PyObject is a Python list, then JavaScript can directly call its append method

Functions also can be freely passed between the two interpreters. Every time a cross-language function is called, it is executed by the corresponding interpreter. Their arguments will be converted according to the same rules.