Skip to content Skip to sidebar Skip to footer

Electron-builder Is Not Bundling The Python Files

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 bund

Solution 1:

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.


Reason

Actually electron-builderIS bundled your python file, but due to asar, your python-shell can NOT find your python file, so cause the error.

How to fix

Solution 1:

Easiest but not recommend by official: disable asar

How to disable asar

change you package.json to:

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

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');
});

should working now.

internal logic

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

that is, .app file structure is:

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

Solution 2:

keep enable asar, put extra files into unpack:

how to asar unpack

change you package.json to:

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

the packaged .app file structure is:

|_ 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.

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


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

Solution 2:

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":["from":"source path","to":"some directory name"]

for more info refer here

Post a Comment for "Electron-builder Is Not Bundling The Python Files"