0

I'm coding my first app using ElectronJs as a GUI for an older project written in python. When i run my Electron app from the cmd i can successfully activate a python venv using the provided script, run the code written in python and deactivate the venv once the script has exported is results as a file in the out/ folder. All of these actions are handled using the child_process.spawn() method and launching a powershell instance with my command as parameter (I don't know if there is a bettere way but this works for me!).

When i try to package a portable executable of my Electron app with electron-builder i know that my python manager script written in js runs but my python code doesn't seem to run and i think that somehow my relative paths no longer work.

I use the following path for my python script:

const pythonScriptPath = path.join(__dirname, '../python/main.py');

Project structure:

root:
 + // other files 
 + src:
    |
    + js: //folder with all my js files
    |
    + html: // folder for html
    + css:  // fodlder for css
    |
    + py: // all my python stuff in here
       + venv: // folder for my python env
       + // all my python scripts

I need a way to correctly locate my files so my application can run correctly when exported to a portable executable

Edit

I could use pyInstaller to convert my python code to an executable so i don't really have to deal with all the venv stuff but i don't think this will make my life any easier since i'm not able to execute files (or programs with file paths as argument)

1

1 Answer 1

0

this is not about paths being relative, but about how the asar archives work. child_process.spawn() simply calls system APIs, and your system doesn't have a way to execute the files from inside your app.asar. python doesn't know how to execute code from there either.

a few solutions to put them outside the asar:

  1. extraResources

put this in your electron-builder config (assuming json):

"extraResources": ["src/py"],

do not pack it another time in files, there's no reason to. this way, your src/py is copied into resources/src/py in your electron-builder build.

you can also put it in a different directory, like this:

"extraResources": [{
  "from": "src/py",
  "to": "py"
}],
  1. extraFiles

this works the same as extraResources, with the difference that it's put next to the electron binary instead of resources subdirectory.

  1. asarUnpack

you can pack your python code as usual into the asar, as specified in files, and use asarUnpack to also put an unpacked copy in resources/app.asar.unpacked, like this:

"asarUnpack": ["src/py"]

I don't recommend this way, because this way your app includes 2 copies of the python code: one is inside the asar (unused by anything), and the other is in app.asar.unpacked

1
  • I tried multiple times to use the solution you suggested but it doesn't seem to be working at all :/
    – roto65
    Commented Sep 4, 2023 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.