electron-builder没有绑定python文件

djmepvbi  于 11个月前  发布在  Electron
关注(0)|答案(3)|浏览(159)

这是我的目录结构,其中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: [...]中的所有东西都放在build > files下的package.json中,然后运行npm run dist。我还试着把python文件显式复制到dist文件夹中,然后运行npm run dist。没有一个工作。
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电子构建器

3bygqnnd

3bygqnnd1#

Please makeure 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,所以请确保不是您的错别字。如果是,请更正。

原因

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

如何修复

解决方案一:

最好但官方不推荐:禁用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
                ...

方案二:

继续启用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运行

eivnm1vs

eivnm1vs2#

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

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

字符串
如果你想把这些文件通过创建目录然后使用extraResources

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


更多信息,请参阅here

wnvonmuf

wnvonmuf3#

请在package.json中使用作者作为发布者信息

{
"name": "AppName",
  "version": "1.0.0",
  "description": "App description",
  "author": {
    "name": "Publisher Name",  // Enter your publisher Name Here
    "email": "publusher-email-address" // Enter Publisher Email
  }
}

字符串

相关问题