相关文章推荐
大鼻子的单车  ·  VSCode常用插件之Live ...·  4 月前    · 
老实的电影票  ·  Java ...·  7 月前    · 
老实的鸵鸟  ·  An invalid request ...·  1 年前    · 
博学的篮球  ·  CheckBox 类 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

@dev is right you need to user the extraResources attribute to write the python files into a external folder. The files inside the asar archive are readonly, so even if you can read the file the pyc cannot be created. Check the asar limitations here – ChesuCR Nov 20, 2018 at 10:30

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

    I too have the same problem @ishandutta2007 has posted. So the first solution you have mentioned seems to fix the issue because by 'asar:false' you don't let the builder bundle the source code at all. Hence the scripts are located without any issue. The second solution also doesn't seem right[no offense : )] to me because you just exclude those files from bundling. Any user who installs the application can view your scripts. I'm still searching for a sleek solution where I can make my caller point to the bundled python scripts. – NIV Mar 9, 2021 at 13:30

    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

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.