简体   繁体   English

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

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

This is my directory structure where renderer.js is included by index.html .这是我的目录结构,其中renderer.js包含在index.html The python scripts visitor.py and download.py are called from renderer.js via python-shell . python 脚本visitor.pydownload.py是通过python-shellrenderer.js调用的。 Once I bundle, it is not able to find the python scripts一旦我捆绑,它就无法找到 python 脚本

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

I tried putting everything in files: [...] in package.json under build > files and then ran npm run dist .我尝试将所有内容放入files: [...] in package.jsonbuild > files然后运行npm run dist I also tried copying python files to dist folder explicitly and then ran npm run dist .我还尝试将 python 文件显式复制到dist文件夹,然后运行npm run dist None are working.没有人在工作。

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

This is my package.json这是我的 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: This is the electron-builder I am talking about https://github.com/electron-userland/electron-builder PS:这是我说的电子生成器https://github.com/electron-userland/electron-builder

Please makesure not your typo请确保不是您的错字

/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 is remderer.js , but other place is renderer.js , so please makesure is NOT your typo. /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 ,所以请确保不是你的错字。 If is, correct it.如果是,请更正。


Reason原因

Actually electron-builder IS bundled your python file, but due to asar , your python-shell can NOT find your python file, so cause the error.其实electron-builder捆绑你的Python文件,但由于asar ,你python-shell无法找到你的Python文件,所以会导致错误。

How to fix怎么修

Solution 1:解决方案1:

Easiest but not recommend by official: disable asar最简单但官方不推荐:禁用 asar

How to disable asar如何disable asar

change you package.json to:将您的package.json更改为:

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

then in your renderer.js , which contain python-shell code, maybe like this:然后在包含python-shell代码的renderer.js ,可能是这样的:

import {PythonShell} from 'python-shell';

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

should working now.现在应该工作。

internal logic内在逻辑

after disable asar, the all related file path is not contain asar, become this:禁用asar后,所有相关文件路径不包含asar,变成这样:

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

that is, .app file structure is:也就是说, .app文件结构是:

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

Solution 2:解决方案2:

keep enable asar, put extra files into unpack :保持启用 asar,将额外的文件放入unpack

how to asar unpack如何解压

change you package.json to:将您的package.json更改为:

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

the packaged .app file structure is:打包的.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

your renderer.js , maybe NOT need change , and should working.您的renderer.js也许不需要更改,并且应该可以工作。

more details about asarUnpack please refer official doc: Overridable per Platform Options有关asarUnpack更多详细信息,请参阅官方文档: Overridable per Platform Options


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

You need to specify them as follows:您需要按如下方式指定它们:

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

if you want to put those files by creating directory then use extraResources如果您想通过创建目录来放置这些文件,请使用 extraResources

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

for more info refer here有关更多信息,请参阅此处

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

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