【问题标题】:Python and Node Js (what to do if the client doesn't have python)Python和Node Js(如果客户端没有python怎么办)
【发布时间】:2023-04-05 06:52:01
【问题描述】:

我正在尝试使用 NodeJs(电子)制作桌面应用程序,并且我希望能够使用 Node 中的子进程实现 python 脚本,但我想知道当用户不使用时我可以做些什么来控制在他们的机器上安装了 Python。无需手动将python安装到客户端的任何方式,或者如果您安装了不同的版本,则不会发生冲突;还是编译 .py 以便不需要 Python?

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

const process = spawn("python", ["fileController.py"]);

process.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});

process.stderr.on("data", (data) => {
console.error(`stderr: ${data}`);
});

process.on("close", (code) => {
console.log(`Exited with code ${code}`);
});

【问题讨论】:

    标签: javascript python node.js python-3.x npm


    【解决方案1】:

    我认为更好的方法是制作一个脚本来检查系统是否具有所有依赖项,包括 python 环境。我可以附上一段示例代码,它可能会帮助您实现它:

    const pyVersion = (cb) => {
        var spawn = require('child_process').spawn('python1', ['-V']);
        spawn.on('error', function(err){
            console.log(err);
            return cb(err, null);
        });
    
        spawn.stderr.on('data', function(data) {
            cb(null, data.toString());
        });
    }
    
    pyVersion((err,version) => {
        if (err) {
            console.log(`Error is ${err}`);
            // ask user to download it or anything
        }
        else {
            console.log(`Version is ${version}`);
            // run the script, you might check the specific version exists or not
        }
    })
    

    在生成 python 进程之前运行此代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多