繁体   English   中英

电子生成器没有捆绑 python 文件

[英]electron-builder is not bundling the python files

这是我的目录结构,其中renderer.js包含在index.html python 脚本visitor.pydownload.py是通过python-shellrenderer.js调用的。 一旦我捆绑,它就无法找到 python 脚本

  |_ index.html
  |_ styles.css
  |_ main.js
  |_ package.json
  |_ dist/
  |_ node_modules/
  |_ renderer.js
  |_ visitor.py
  |_ download.py

我尝试将所有内容放入files: [...] in package.jsonbuild > files然后运行npm run dist 我还尝试将 python 文件显式复制到dist文件夹,然后运行npm run dist 没有人在工作。

/Application/test.app/Contents/Resources/app.asar/remderer.js:226 错误:python:无法打开文件“visitor.py”:[错误 2] 没有那个文件或目录

这是我的 package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "pack": "build --dir",
    "dist": "build"
  },
  "author": "",
  "license": "ISC",
  "build": {
    "appId": "com.example.app",
    "files": [
      "dist/",
      "node_modules/",
      "index.html",
      "main.js",
      "package.json",
      "renderer.js",
      "styles.css",
      "visitor.py",
      "download.py"
    ],
    "dmg": {
      "contents": [
        {
          "x": 110,
          "y": 150
        },
        {
          "x": 240,
          "y": 150,
          "type": "link",
          "path": "/Applications"
        }
      ]
    },
    "linux": {
      "target": [
        "AppImage",
        "deb"
      ]
    },
    "win": {
      "target": "squirrel",
      "icon": "build/icon.ico"
    }
  },
  "dependencies": {
    "csv-parse": "^2.5.0",
    "electron-css": "^0.6.0",
    "npm": "^6.1.0",
    "python-shell": "^0.5.0",
  },
  "devDependencies": {
    "electron": "^2.0.3",
    "electron-builder": "^20.19.1"
  }
}

PS:这是我说的电子生成器https://github.com/electron-userland/electron-builder

请确保不是您的错字

/Application/test.app/Contents/Resources/app.asar/remderer.js:226 Error: python: can't open file 'visitor.py': [Error 2] No such file or directoryremderer.js ,但是另一个地方是renderer.js ,所以请确保不是你的错字。 如果是,请更正。


原因

其实electron-builder捆绑你的Python文件,但由于asar ,你python-shell无法找到你的Python文件,所以会导致错误。

怎么修

解决方案1:

最简单但官方不推荐:禁用 asar

如何disable asar

将您的package.json更改为:

{
...
  "build": {
    "appId": "com.example.app",
...
    "asar": false,

然后在包含python-shell代码的renderer.js ,可能是这样的:

import {PythonShell} from 'python-shell';

PythonShell.run('visitor.py', null, function (err) {
  if (err) throw err;
  console.log('finished');
});

现在应该工作。

内在逻辑

禁用asar后,所有相关文件路径不包含asar,变成这样:

  • /Application/test.app/Contents/Resources/app/visitor.py
  • /Application/test.app/Contents/Resources/app/renderer.js

也就是说, .app文件结构是:

|_ test.app
    |_ Contents
        |_ Resources
            |_ app
                |_ styles.css
                |_ main.js
                |_ package.json
                |_ dist/
                |_ node_modules/
                |_ renderer.js
                |_ visitor.py
                |_ download.py
                ...

解决方案2:

保持启用 asar,将额外的文件放入unpack

如何解压

将您的package.json更改为:

{
...
  "build": {
    "appId": "com.example.app",
...
    "asar": true,
    "asarUnpack": [
      "visitor.py",
      "download.py"
      "renderer.js"
    ],

打包的.app文件结构是:

|_ test.app
    |_ Contents
        |_ Resources
            |_ app.asar             # a single compressed binary file
            |_ app.asar.unpacked    # a folder/directory, contain unpacked origin files
                |_ visitor.py
                |_ download.py
                |_ renderer.js

您的renderer.js也许不需要更改,并且应该可以工作。

有关asarUnpack更多详细信息,请参阅官方文档: Overridable per Platform Options


PS:其他一些asar和相关的尝试,可以参考我的中文帖子: 【已解决】mac中PyInstaller打包后的二进制文件在electron-builder打包后app中某些无法通过child_process的execFile运行

您需要按如下方式指定它们:

    "extraFiles": [
        "from":"source path",
        "to":"your destination"
    ]

如果您想通过创建目录来放置这些文件,请使用 extraResources

    "extraResources": [
        "from":"source path",
        "to":"some directory name"
    ]

有关更多信息,请参阅此处

暂无
暂无

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

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