Only tested with python 3.6.x and python 3.7.x. Probably wont work with Python 2.7
BEFORE NPM INSTALL OR YARN INSTALL
- Make sure
python
in your systemPATH
is the correct one:python --version
- Linux and Mac only: make sure
python-config
is also in your path and is from the correct python installation. You may have to symlinkpython3-config
to/usr/local/bin
. yarn add @fridgerator/pynode
ornpm install @fridgerator/pynode
In a python file test.py
:
def add(a, b):
return a + b
in node:
const pynode = require('@fridgerator/pynode')
// Workaround for linking issue in linux:
// https://bugs.python.org/issue4434
// if you get: `undefined symbol: PyExc_ValueError` or `undefined symbol: PyExc_SystemError`
pynode.dlOpen('libpython3.6m.so') // your libpython shared library
// optionally pass a path to use as Python module search path
pynode.startInterpreter()
// add current path as Python module search path, so it finds our test.py
pynode.appendSysPath('./')
// open the python file (module)
pynode.openFile('test')
// call the python function and get a return value
let x = pynode.call('add', 1, 2)
x === 3 // true