I'm trying to run a python script with an input and an output when the user submits a form. The form activates the post function in express and I have used spawn of child_process to run the script. However, ModuleNotFoundError: No module named 'numpy' happens.
This is my index.js code.
const spawn = require('child_process').spawn;
router.post('/testing', function (req, res) {
//res.send('POST request to the homepage')
const pythonProcess = spawn('python', ["./public/models/main.py", [1, 2, 3]]);
let output;
pythonProcess.stdout.on('data', (data) => {
// Do something with the data returned from python script
output+=data;
});
pythonProcess.on("end", () => {
console.log(output.toString())
res.write(output);
})
pythonProcess.stderr.on("data", (data) => {
console.error(data.toString())
})
})
and this is my python code
import sys
import numpy as np
def multiply_matrix(matrix):
return matrix * 2
def main():
matrix = multiply_matrix(sys.argv[1])
print(matrix)
sys.stdout.flush()
if __name__ == "__main__":
main()
How can I fix this? I need this to develop a larger program with more complicated computations using python.