
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
node-calls-python
Advanced tools
This module lets you run python code inside node without spawning new processes
npm install node-calls-python
Sometimes you have to install prerequisites to make it work.
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt install nodejs
sudo apt install python3
sudo apt install python3-dev
sudo apt install make
sudo apt install g++
sudo npm install -g node-gyp
npm install --global --production windows-build-tools
npm install -g node-gyp
Let's say you have the following python code in test.py
import numpy as np
def multiple(a, b):
return np.multiply(a, b).tolist()
Then to call this function directly you can do this in Node
const nodecallspython = require("node-calls-python");
let py = nodecallspython.interpreter;
py.import("test.py").then(async function(pymodule) {
let result = await py.call(pymodule, "multiple", [1, 2, 3, 4], [2, 3, 4, 5]);
console.log(result);
});
Or to call this function by using the synchronous version
const nodecallspython = require("node-calls-python");
let py = nodecallspython.interpreter;
py.import("test.py").then(async function(pymodule) {
let result = py.callSync(pymodule, "multiple", [1, 2, 3, 4], [2, 3, 4, 5]);
console.log(result);
});
Let's say you have the following python code in test.py
import numpy as np
class Calculator:
vector = []
def __init__(self, vector):
self.vector = vector
def multiply(self, scalar, vector):
return np.add(np.multiply(scalar, self.vector), vector).tolist()
Then to instance the class directly in Node
const nodecallspython = require("node-calls-python");
let py = nodecallspython.interpreter;
py.import("test.py").then(async function(pymodule) {
let pyobj = await py.create(pymodule, "Calculator", [1.4, 5.5, 1.2, 4.4]);
let result = await py.call(pyobj, "multiply", 2, [10.4, 50.5, 10.2, 40.4]);
});
Or to instance the class synchronously and directly in Node
const nodecallspython = require("node-calls-python");
let py = nodecallspython.interpreter;
py.import("test.py").then(async function(pymodule) {
let pyobj = py.createSync(pymodule, "Calculator", [1.4, 5.5, 1.2, 4.4]);
let result = await py.callSync(pyobj, "multiply", 2, [10.4, 50.5, 10.2, 40.4]); // you can use ayns version (call) as well
});
Let's say you have the following python code in logreg.py
from sklearn.datasets import load_iris, load_digits
from sklearn.linear_model import LogisticRegression
class LogReg:
logreg = None
def __init__(self, dataset):
if (dataset == "iris"):
X, y = load_iris(return_X_y=True)
else:
X, y = load_digits(return_X_y=True)
self.logreg = LogisticRegression(random_state=42, solver='lbfgs', multi_class='multinomial')
self.logreg.fit(X, y)
def predict(self, X):
return self.logreg.predict_proba(X).tolist()
Then you can do this in Node
const nodecallspython = require("node-calls-python");
let py = nodecallspython.interpreter;
py.import("logreg.py")).then(async function(pymodule) { // import the python module
let logreg = await py.create(pymodule, "LogReg", "iris"); // create the instance of the classifier
let predict = await py.call(logreg, "predict", [[1.4, 5.5, 1.2, 4.4]]); // call predict
console.log(predict);
});
If you get an error like this while trying to call Python code
ImportError: /usr/local/lib/python3.7/dist-packages/cpython-37m-arm-linux-gnueabihf.so: undefined symbol: PyExc_RuntimeError
You can fix it by passing the name of your libpython shared library to fixlink
const nodecallspython = require("node-calls-python");
let py = nodecallspython.interpreter;
py.fixlink('libpython3.7m.so');
- undefined to None
- null to None
- boolean to boolean
- number to double or long (as appropriate)
- int32 to long
- uint32 to long
- int64 to long
- string to unicode (string)
- array to list
- object to dictionary
- None to undefined
- boolean to boolean
- double to number
- long to int64
- unicode (string) to string
- list to array
- tuple to array
- set to array
- dictionary to object
FAQs
This module lets you run python code inside node without spawning new processes
The npm package node-calls-python receives a total of 7,245 weekly downloads. As such, node-calls-python popularity was classified as popular.
We found that node-calls-python demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.