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

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

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

我尝试将所有内容放在files: [...]下的package.json中的build > 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 directory是remderer.js,但其他地方是renderer.js,因此请确保不是您的错字。如果是,请更正它。

原因

实际上electron-builder是捆绑了您的python文件,但是由于asar,您的python-shell找不到您的python文件,因此会导致错误。

怎么修

解决方案1:

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

如何disable 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

如何打开包装

将您的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:在不更改段落顺序的情况下在文件的每个段落中反向单词? - python

我想通过反转text_in.txt文件中的单词来生成text_out.txt文件,如下所示:text_in.txt具有两段,如下所示:Hello world, I am Here. I am eighteen years old. text_out.txt应该是这样的:Here. am I world, Hello old. years eighteen a…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何在python中将从PDF提取的文本格式化为json - python

我已经使用pyPDF2提取了一些文本格式的发票PDF。我想将此文本文件转换为仅包含重要关键字和令牌的json文件。输出应该是这样的:#PurchaseOrder {"doctype":"PO", "orderingcompany":"Demo Company", "su…

我怎样才能从字典的键中算出对象? - python

我有这本字典:dict={"asset":[("S3","A1"),"S2",("E4","E5"),("E1","S1"),"A6","A8"], "…

Python:将两列组合在一起,找到第三列的总和 - python

python真的很新,需要我完成的问题需要一些帮助。我需要根据用户对月份(MM)和年份(YYYY)的输入来找到每个时间段(月/年)的平均收入。我的输入如下:year_value = int(input("Year (YYYY): ")) month_value = int(input("Month (MM): ")) …