python - Electron-builder 没有捆绑 python 文件

标签 python node.js python-3.x electron electron-builder

这是我的目录结构,其中 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

我尝试将所有内容放入 build > files 下的 package.json 中的 files: [...] 中,然后运行 ​​ npm run dist。 我还尝试将 python 文件显式复制到 dist 文件夹,然后运行 ​​npm run dist。 没有一个在工作。

/Application/test.app/Contents/Resources/app.asar/remderer.js:226 Error: python: can't open file 'visitor.py': [Error 2] No such file or directory

这是我的 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: 这就是我所说的 electron-builder https://github.com/electron-userland/electron-builder

最佳答案

请确保没有拼写错误

/Application/test.app/Contents/Resources/app.asar/remderer.js:226 错误:python:无法打开文件“visitor.py”:[错误 2] 没有这样的文件或目录remderer.js,但其他地方是 renderer.js,所以请确保不是你的拼写错误。如果是,请更正。

<小时/>

原因

实际上,electron-builderIS捆绑了你的python文件,但是由于asar,你的python-shell找不到你的Python文件,所以导致错误。

如何修复

解决方案1:

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

如何禁用 asar

package.json 更改为:

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

然后在您的 renderer.js 中,其中包含 python-shell 代码,可能如下所示:

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:

如何解压asar

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运行

关于python - Electron-builder 没有捆绑 python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51182226/

相关文章:

python - Python 中类似 C 的结构数组

node.js - 为什么我应该为 ReactJS 创建单独的项目?

python - 尝试将网页中的JS数据输出到.html输出文件中

python - Sqlite3 数据库正在添加不应添加的条目

python - 如何将 django admin "view site"链接更改为自定义绝对 url

python - 警告 :tensorflow - initialize_all_variables (from tensorflow. python.ops.variables) 已弃用

python - 在 Python 中使用 ARN iam 将文件上传到 Amazon s3 存储桶

python - 如何提取 m × m 矩阵中的每个 n × n 矩阵

javascript - 从客户语音中提取字母数字字符串

javascript - 使用 ElasticSearch 7.x 进行精确搜索