Including python script in packaged electron app
Question:
After packaging my app for a linux system, the python script for doing some background tasks fails to execute, probably because it is not packaged correctly. I’ve looked through several threads now, stumbled over ASAR, extraResources etc. but I’m not really getting it to work.
My folder structure is the following
- App.Root
|-public
| -background.html
|
|-scripts
-python.py
This is the content of my background.html:
<script>
const { PythonShell } = require('python-shell');
const { ipcRenderer } = require('electron');
const path = require('path');
let pyshell
ipcRenderer.on('START_PROCESSING', (event, args) => {
console.log("Loading Python")
const { data } = args;
pyshell = new PythonShell(path.join(__dirname, '/../scripts/python.py'), {
pythonPath: 'python3',
pythonOptions:['-u'],
args: [data]
});
pyshell.on('message', function (results) {
ipcRenderer.send('MESSAGE_FROM_BACKGROUND', { message: results });
});
});
ipcRenderer.on('QUIT_PYTHON', (event, args) => {
pyshell.kill();
ipcRenderer.send('CLOSE_BACKGROUND');
});
ipcRenderer.send('BACKGROUND_READY');
I already tried including the scripts folder like this and I think it worked since it appeared in the "dist" folder but dont know where to go from here:
"extraResources": [
{
"from": "scripts",
"to": "scripts"
}
I understand that this is an issue with paths and how electron is packaging the app, but since I`m new to all of this I feel a bit overwhelmed. People posted a bunch of different solutions which weren’t working for me, or the problem was never solved.
I’d be glad if somebody who’s familiar with packaging electron apps can chime in.
//EDIT:
This is my current build{} config:
"build": {
"files": [
"build/**/*",
"node_modules/**/*"
],
"asarUnpack":"./scripts/**",
"extraResources": "./scripts/**",
"publish": {
"provider": "github",
"repo": "test",
"owner": "Test"
}
},
I also changed:
pyshell = new PythonShell(path.join(__dirname, '/../scripts/python.py')
To:
pyshell = new PythonShell(path.join(process.resourcesPath, '/scripts/python.py')
cause it was obivously wrong.
Answers:
I am also using Python scripts in my Electron application.
i need see your entire build {} config to see better, but i think that the issue is because your asar doesn’t include correctly your ./script
folder
In my electron-builder config i have this
"asarUnpack": "./scripts/**",
"extraResources": ["./scripts/**", "./scripts/database/**"], //Note that I also include a folder named database inside my ./script folder
With this lines my folder ./scripts
is include correctly
EDIT: //
My block with a script that loging me in my app:
ipcMain.on(`login`, async (event, args) => {
let command = ``;
if (process.platform === `linux`) {
command = await exec(`./scripts/login ${args.user} ${args.password}`);
}
command.stdout.on(`data`, (data) => {
// do something when scripts runs
command.on(`exit`, (code) => {
console.log(code);
});
After some more hours of research and troubleshooting I’d like to post my answer here: Because of my own incompetence I missed that python-shell actually has an error output. After activating this I realized that the script was in fact bundled and loaded. However python couldn’t execute because apparently a module that I imported was not installed. Then it hit me.
I changed
pythonPath: 'python3',
To:
pythonPath: 'python',
Now it works. Maybe this will help somebody in the future.
After packaging my app for a linux system, the python script for doing some background tasks fails to execute, probably because it is not packaged correctly. I’ve looked through several threads now, stumbled over ASAR, extraResources etc. but I’m not really getting it to work.
My folder structure is the following
- App.Root
|-public
| -background.html
|
|-scripts
-python.py
This is the content of my background.html:
<script>
const { PythonShell } = require('python-shell');
const { ipcRenderer } = require('electron');
const path = require('path');
let pyshell
ipcRenderer.on('START_PROCESSING', (event, args) => {
console.log("Loading Python")
const { data } = args;
pyshell = new PythonShell(path.join(__dirname, '/../scripts/python.py'), {
pythonPath: 'python3',
pythonOptions:['-u'],
args: [data]
});
pyshell.on('message', function (results) {
ipcRenderer.send('MESSAGE_FROM_BACKGROUND', { message: results });
});
});
ipcRenderer.on('QUIT_PYTHON', (event, args) => {
pyshell.kill();
ipcRenderer.send('CLOSE_BACKGROUND');
});
ipcRenderer.send('BACKGROUND_READY');
I already tried including the scripts folder like this and I think it worked since it appeared in the "dist" folder but dont know where to go from here:
"extraResources": [
{
"from": "scripts",
"to": "scripts"
}
I understand that this is an issue with paths and how electron is packaging the app, but since I`m new to all of this I feel a bit overwhelmed. People posted a bunch of different solutions which weren’t working for me, or the problem was never solved.
I’d be glad if somebody who’s familiar with packaging electron apps can chime in.
//EDIT:
This is my current build{} config:
"build": {
"files": [
"build/**/*",
"node_modules/**/*"
],
"asarUnpack":"./scripts/**",
"extraResources": "./scripts/**",
"publish": {
"provider": "github",
"repo": "test",
"owner": "Test"
}
},
I also changed:
pyshell = new PythonShell(path.join(__dirname, '/../scripts/python.py')
To:
pyshell = new PythonShell(path.join(process.resourcesPath, '/scripts/python.py')
cause it was obivously wrong.
I am also using Python scripts in my Electron application.
i need see your entire build {} config to see better, but i think that the issue is because your asar doesn’t include correctly your ./script
folder
In my electron-builder config i have this
"asarUnpack": "./scripts/**",
"extraResources": ["./scripts/**", "./scripts/database/**"], //Note that I also include a folder named database inside my ./script folder
With this lines my folder ./scripts
is include correctly
EDIT: //
My block with a script that loging me in my app:
ipcMain.on(`login`, async (event, args) => {
let command = ``;
if (process.platform === `linux`) {
command = await exec(`./scripts/login ${args.user} ${args.password}`);
}
command.stdout.on(`data`, (data) => {
// do something when scripts runs
command.on(`exit`, (code) => {
console.log(code);
});
After some more hours of research and troubleshooting I’d like to post my answer here: Because of my own incompetence I missed that python-shell actually has an error output. After activating this I realized that the script was in fact bundled and loaded. However python couldn’t execute because apparently a module that I imported was not installed. Then it hit me.
I changed
pythonPath: 'python3',
To:
pythonPath: 'python',
Now it works. Maybe this will help somebody in the future.