I am calling a python script with PythonShell on javascript.
my javascript function is below
function runPythonScript(pythonPath, pythonScript) {
console.log('ND - server - run python without argument');
let myPath = __dirname;
console.log('myPath: ', myPath);
//let pythonPath = pythonPath;
console.log('pythonPath: ', pythonPath);
//let pythonScript = pythonScript;
console.log('pythonScript: ', pythonScript);
//Here are the option object in which arguments can be passed for the python_test.js.
let options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
scriptPath: myPath + pythonPath //If you are having python_test.py script in same folder, then it's optional.
//args: ['xxxx'] //An argument which can be accessed in the script using sys.argv[1]
};
return new Promise(function(resolve, reject) {
PythonShell.run(pythonScript, options, function(err, result) {
if (err) reject(err);
// result is an array consisting of messages collected
//during execution of script.
console.log('data: ', result.toString());
let parsedData = JSON.parse(JSON.stringify(result.join('\n')));
console.log('data type: ', typeof parsedData, ' of length ', parsedData.length);
parsedData = parsedData.replace(/'/g, `"`); // replace the quote by the double quotes for parsing the string to a JSON object.
parsedData = JSON.parse(parsedData);
console.log('parsed data type: ', typeof parsedData);
//console.log('parsed data: ', parsedData);
resolve(parsedData);
});
});
}
async function main() {
try {
let counterpartyData = await runPythonScript('/python','test.py');
console.log('ND - server - counterparty data: ', counterpartyData);
}
catch (e) {
console.error(e);
}
}
main();
In this function I call the python script here below
import cv2
mydict ={
"counterParty_Car": "Ford",
"counterParty_CarPlate" : "O XXX YYY",
"counterParty_Name" : "John Snow",
"counterParty_Policy": "0123456789"
}
print(mydict)
If i remove the import cv2 in the python script, there is no error. But if I want to add the import cv2 (because i needed for the next steps). An error appears meaning that there is no module cv2.
/app/node_modules/libheif-js/libheif/libheif.js:1
2022-01-12T15:14:56.167971+00:00 app[web.1]: var Module=typeof Module!=="undefined"?Module:{};((function(){var
[...]
I tried the answers proposed in other questions and other website but nothing works. Do you have an idea how to solve this issue?
in my requirment.txt heroku file, i have opencv-python-headless==4.5.4.60