繁体   English   中英

如何在 heroku 上从 nodejs 运行 python 脚本

[英]How to run python script from nodejs on heroku

我基本上正在开发一个具有一些按钮的 NodeJS 应用程序,单击它们我会运行一些 python 机器学习和深度学习模型,但问题是我无法安装基本的 python 库,例如 numpy。 此外,一旦我将 python 构建包添加到我的应用程序中,它就会停止运行。 日志显示没有 web 进程。 我在文档中发现我们必须使用 npm 命令插入 Procfile.txt,但这也没有解决问题。 整个设置在本地机器上完美运行。

你需要使用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/

或者你可以使用这个 npm 库python

@Vladim Hulevich 感谢您的回复。 但我只是这样运行它,它在我的本地机器上就像一个魅力,但是当我在 heroku 上运行我的脚本时,它会产生 numpy 未找到的错误。 但是我的应用程序基本上是 nodejs 所以我只添加了 nodejs buildpack 而不是 python 因为当我添加 python buildpack 我的 nodejs 应用程序也停止运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM