How to run python script from nodejs on heroku

Viewed 441

I am basically working on a NodeJS app that has some buttons and on clicking them I run some python machine learning and deep learning models but the problem is that I cant install basic python libraries like numpy. Also as soon as I add python build pack to my app it stops running. The logs show that there is no web process. I found in docs that we have to insert a Procfile.txt with npm commands but that also didn't solve the problem. The whole setup runs perfectly on the local machine.

2 Answers

you need use child_process;

const { exec } = require("child_process");

exec("python path/to/the/python_script.py", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

https://stackabuse.com/executing-shell-commands-with-node-js/

Or you can use for this npm library python

@Vladim Hulevich Thanks for replying. But I ran it like this only and it works like a charm on my local machine but when I run my script on heroku it produces error of numpy not found. But my app is basically nodejs so I only added nodejs buildpack and not python as when I add python buildpack my nodejs app also stops running

Related