Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Error: python: can't open file 'visitor.py': [Error 2] No such file or directory

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: This is the electron-builder I am talking about https://github.com/electron-userland/electron-builder

like image 512
ishandutta2007 Avatar asked Oct 29 '25 13:10

ishandutta2007


1 Answers

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-builder IS 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运行

like image 107
crifan Avatar answered Nov 01 '25 05:11

crifan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!