【问题标题】:pythonPath for python-shell is not working after when electron is build构建电子后,python-shell 的 pythonPath 不起作用
【发布时间】:2020-07-17 11:39:17
【问题描述】:

我正在使用电子应用程序使用 python-shell 运行 python 脚本。我已将 pythonPath 设置为我的 anaconda 环境,如下所示。这在我运行应用程序时可以找到,但在使用 electron-builder 打包应用程序后,它显示以下错误:没有找到这样的文件或目录即使我在同一台 PC 上使用绝对路径。

const {PythonShell} = require('python-shell');
const path = require('path');

const options = {
    mode: 'text',
    pythonPath: 'C:/Users/nauma/.conda/envs/YOLO/python.exe',
    pythonOptions: ['-u'], // get print results in real-time
    scriptPath: path.join(__dirname, '/build/engine/')
  };

  PythonShell.run('index.py', options, function (err, res) {
    if (err) throw err;
    console.log(res);
  });

构建应用后的异常如下所示:

[

【问题讨论】:

    标签: python electron electron-builder


    【解决方案1】:
    • 不设置pythonPath时在windows上,

      python shell可以自己找到python的路径,

      可能使用环境变量或注册表项。

    • 但是为了确保我的电子应用程序可以在任何地方运行,

      我更喜欢使用特定配置嵌入 python

      电子生成器捆绑python

    package.json:

    "build": {
        "appId": "angular_electron.id",
        "extraFiles": [
          {
            "from": "resources/${os}",
            "to": "Resources",
          }
        ]
      }
    

    并使用正确的资源路径调用 python:

    import { isPackaged } from 'electron-is-packaged';
    const resourcesPath = isPackaged
        ? join(rootPath, './resources')
        : join(rootPath, './resources', getPlatform());
    
    pyshell.PythonShell.run('hello.py', 
        {pythonPath:join(resourcesPath,'/python-3.6.3-embed-amd64/python.exe'),scriptPath:join(resourcesPath,'/bin')}, 
        function  (err, results)  {
          if  (err)  throw err;
          console.log('hello.py finished.');
          console.log('results:', results);
         });
    

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 2018-04-25
      • 2020-03-14
      • 2015-10-31
      • 2021-06-20
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多