【问题标题】:python-shell with electron-builder work fine in my computer but not working in other computer带有电子生成器的 python-shell 在我的计算机上工作正常,但在其他计算机上不工作
【发布时间】:2020-06-07 06:15:16
【问题描述】:

这是我在 electron.js 中的 PythonSHELL 脚本:

const {PythonShell} = require('python-shell');
    const path = require('path');
    const options = {
        mode: 'text',
        pythonPath: 'C:/Users/abdou/AppData/Local/Programs/Python/Python38/python.exe',

        scriptPath: isDev ?  `${path.join(__dirname, "db")}`: `${path.join(__dirname, "../../build/db")}`,
    };
    mainWindow.webContents.on('did-finish-load', ()=>{
    PythonShell.run('p.py', options, function (err, results) {
        if (err) throw err;
        // results is an array consisting of messages collected during execution
         mainWindow.webContents.send('data', results[0]);
        })

    });

这是我的 package.json 脚本标签:

 "react-start": "react-scripts start",
"react-build": "react-scripts build",
"react-test": "react-scripts test --env=jsdom",
"react-eject": "react-scripts eject",
"electron-build": "electron-builder",
"release": "yarn react-build && electron-builder --publish=always",
"build": "yarn react-build && yarn electron-build",
"start": "concurrently \"cross-env BROWSER=none yarn react-start\" \"wait-on http://localhost:3000 && electron .\""

这是构建标签:

 "build": {
"appId": "com.gs_client_descktop",
"asar": false,
"extraResources": [
  "**/db/**/*"
]}

消息错误是

Uncaught Exeption:
    spawn C:/Users/abdou/AppData/Local/Programs/Python/Python38/python.exe ENONENT
    at Process.ChildProcess._handle.onexit(intrnal/child_process.js:264:19)
    at onErrorNT (internal/child_process.js:456:16)
    at processTicksAndRejections(internal/process/task_queues.js:77:11)

谢谢。

【问题讨论】:

  • 这意味着给定路径中的python.exe 不存在,请参阅this answer。您确定两台机器上都安装了 Python 吗?
  • 我不想在两台机器上都安装 python 我只想安装我的应用程序
  • 好吧,那这行不通,因为python-shell 实际上会尝试生成一个 Python 进程。从您的错误消息中可以看出,这会失败,因为 Python 未安装在第二台计算机上。
  • 你能给我一个解决方案,我可以在电子应用程序中使用python,而无需在所有计算机上安装python吗?
  • 你不能,AFAIK。使用 Python 需要一个 Python 解释器,该解释器需要以一种或另一种方式实现,因此最好使用 Python 包/安装程序来发布您的应用程序。

标签: python node.js windows electron electron-builder


【解决方案1】:

答案很明确。您已将 pythonPath 链接到计算机上的解释器。您在那边给出的路径(在这种情况下--> pythonPath: 'C:/Users/abdou/AppData/Local/Programs/Python/Python38/python.exe')只存在于您的计算机上。所以它不应该工作。

作为一种解决方案,您可以在项目中使用 python 虚拟环境。然后将虚拟环境中的解释器链接到 Python 路径。 设置到虚拟环境的路径很可能如下所示:

 pythonPath: path.join(__dirname, '/../python/venv/Scripts/python.exe');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多