2

I have made my own application with electron, and I encounter an issue with production builds, when it comes to launching an external python script.

When I run the code in the dev environment, I have something like:

const out = spawnSync(PYTHON_LOCATION, ['-d', '-j', '-p', path, tempfile]);

and it works well because PYTHON_LOCATION is public/python.py.

However, when I use electron-builder to make an AppImage, this does not work because the script can't obviously be found. An AppImage application is a single file and it's obvious that the public directory is not exported with it.

Is there a solution to this issue?

Thanks!

yhu420
  • 436
  • 6
  • 18

2 Answers2

0

When an AppImage is executed the AppDir variable is set on the process environment so you have to check whether that variable exists and is set. Then you can build the right path to your python script.

Alexis
  • 591
  • 2
  • 8
0

Electron bundles most things into an app.asar file that probably holds the file that you want. The problem is, there are one or two well intentioned suggestions on the web that AppImage files need to be queried relative to the container itself, which is unfortunately a bit misleading. The reality is, no amount of these variations will work as I'm sure you discovered:

./PYTHON_LOCATION
/PYTHON_LOCATION
PYTHON_LOCATION

The solution is that you need to use __dirname to find out where the image is. Its most likely somewhere like this folder when you run the AppImage (but of course, not when you run from the IDE):

/tmp/.mount_YourApplicationNameHVTcDI/

And so __dirname is (assuming the app was built from an app/electron/ folder:

/tmp/.mount_YourApplicationNameHVTcDI/resources/app.asar/app/electron

So your solution will be something like:

Path.join(__dirname, PYTHON_LOCATION)

Even though Electron is a looking at resources in an app.asar file, it is transparent to the file system and just looks like a folder. Hence you can see inside it using normal file I/O commands.

Remember that in the IDE, __dirname will be totally different, so the file will need to be there as well.

Paul F. Wood
  • 1,453
  • 16
  • 19