0.4.7 • Published 6 years ago
pynode-fix v0.4.7
PyNode
Call python code from node.js
Node v10 or above is required
Only tested with python 3.6.x and python 3.7.x. Probably wont work with Python 2.7
Installation
BEFORE NPM INSTALL OR YARN INSTALL
- Make sure
pythonin your systemPATHis the correct one:python --version - Linux and Mac only: make sure
python-configis also in your path and is from the correct python installation. You may have to symlinkpython3-configto/usr/local/bin. yarn add @fridgerator/pynodeornpm install @fridgerator/pynode
Usage
In a python file test.py:
def add(a, b):
return a + bin 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
pynode.call('add', 1, 2, (err, result) => {
if (err) return console.log('error : ', err)
result === 3 // true
})0.4.7
6 years ago