27

I have a Electron project which executes some python script using NodeJS's child_process module. My python script is in the root folder of my project.

Here's how I call the python script:

let py = spawn('python',['ResolvePosition.py', obsFilePath, navFilePath])
py.stdout.on('data', data => console.log('data : ', data.toString()))
py.on('close', ()=>{
  // Python ends, do stuff
})

This works fine if I run my electron app with npm start When I build an executable for Windows using the npm module electron-builder and run the executable from dist/win-unpacked/my-app.exe, this won't work, it seems that my script is not accesible with python ./my-script-py.

So, how can I make this code works for the built project?

Jose Hermosilla Rodrigo
  • 3,513
  • 6
  • 22
  • 38

2 Answers2

22

I've solved my problem. I'll explain for possible future readers with the same problem.

Using electron builder, there are some options possible for not to package the application source code into an electron file. These options are:

asar

Whether to package the application’s source code into an archive, using Electron’s archive format. Defaults to true. Node modules, that must be unpacked, will be detected automatically, you don’t need to explicitly set asarUnpack - please file issue if this doesn’t work.

Or you can pass object of asar options.

asarUnpack

A glob patterns relative to the app directory, which specifies which files to unpack when creating the asar archive.

DOCS

Setting asar to false solves the issue, but it is not recommended by electron-builder.

So including all the files I need to unpack in a folder, and using "asarUnpack" : "my-folder/*" is the right way. Now all the files unpacked are available in /resources/app.asar.unpacked/my-folder

Another thing to take into account is that using the path './ResolvePosition.py' is going to look at the root folder of my electron project, not the path where my NodeJS file is located, I need to use :

let python = spawn('python', [path.join(__dirname, '../app.asar.unpacked/my-folder', 'ResolvePosition.py'), obsFilePath, navFilePath])
Jose Hermosilla Rodrigo
  • 3,513
  • 6
  • 22
  • 38
  • You are assuming the client that is running the app has python installed? Or are you pakacing python with the electron app? – paulovitorjp Jun 04 '18 at 12:42
  • Yes, here im assuming the client has Python installed. (As the client was just me) I'm not sure how to workaround this easily. @paulovitorjp – Jose Hermosilla Rodrigo Jun 04 '18 at 14:57
  • How did you setup the python enviroment in `/resources/app.asar.unpacked/my-folder` after installation. python wont run if python and all the required modules are not present in appropiate version are not found there. – ishandutta2007 Jul 13 '18 at 03:23
18

You can include the script in extraResources:

"build": {
    "extraResources": "python_scripts",
...

And then the dir will be at the app root:

let python = spawn('python', [path.join(app.getAppPath(), '..', 'python_scripts/my_script.py'])
HdN8
  • 2,953
  • 3
  • 24
  • 26
  • 1
    what is the app variable in this case? or where it comes from? I assume path comes from the nodejs built in path module but what is app – basilisk Oct 13 '20 at 21:27
  • `app` comes from requiring the `electron` package. `const { app } = require("electron");` [And here's the docs on app.getAppPath](https://www.electronjs.org/docs/api/app#appgetapppath). – tscizzle Feb 03 '21 at 16:07