0

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.

Alyssa
  • 23
  • 4
  • Command line arguments are always passed as strings. I have no idea what Javscript is doing with `[1,2,3]`. Have you tried just printing the contents of `sys.argv` and returning? – Tim Roberts Aug 03 '21 at 03:23
  • @TimRoberts I found out that the no module error was because I didn't have numpy installed in my python. I changed the argument as strings and tried printing sys.argv[1] but infinite loading occurs. – Alyssa Aug 03 '21 at 03:29

0 Answers0