electron-builder 没有捆绑 python 文件

electron-builder is not bundling the python files(electron-builder 没有捆绑 python 文件)

本文介绍了electron-builder 没有捆绑 python 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的目录结构,其中 renderer.js 包含在 index.html 中.python 脚本 visitor.pydownload.py 通过 renderer.js 调用.com/package/python-shell" rel="nofollow noreferrer">python-shell.一旦我捆绑,它就无法找到 python 脚本

This is my directory structure where renderer.js is included by index.html . The python scripts visitor.py and download.py are called from renderer.js via python-shell. Once I bundle, it is not able to find the python scripts

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

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

I tried putting everything in files: [...] in package.json under build > files and then ran npm run dist. I also tried copying python files to dist folder explicitly and then ran npm run dist. None are working.

/Application/test.app/Contents/Resources/app.asar/remderer.js:226错误:python:无法打开文件visitor.py":[错误 2] 没有这样的文件或目录

/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

This is my 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

PS: This is the electron-builder I am talking about 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,所以请确保不是您的错字.如果是,请更正它.

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. If is, correct it.

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

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.

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

把你的 package.json 改成:

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

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

then in your renderer.js, which contain python-shell code, maybe like this:

import {PythonShell} from 'python-shell';

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

现在应该可以工作了.

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

after disable asar, the all related file path is not contain asar, become this:

  • /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可能不需要更改,应该可以工作.

your renderer.js, maybe NOT need change, and should working.

关于asarUnpack的更多细节请参考官方文档:每个平台选项可覆盖

more details about asarUnpack please refer official doc: Overridable per Platform Options

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

PS: some other asar and related trying, can refer my Chinese post: 【已解决】mac中PyInstaller打包后的二进制文件在electron-builder打包后app中有些无法通过child_process的execFile运行

这篇关于electron-builder 没有捆绑 python 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:electron-builder 没有捆绑 python 文件

基础教程推荐