I am developing an Electron application that executes a Python script using child_process
. While everything runs perfectly in the local development environment, I encounter an error when running the packaged application on macOS. The error occurs when trying to execute the Python executable.
Command failed: "/Volumes/YourAppName 1.0.0-arm64/YourAppName.app/Contents/Resources/resources/python3.10"
/bin/sh: /Volumes/YourAppName 1.0.0-arm64/YourAppName.app/Contents/Resources/resources/python3.10: No such file or directory
Relevant Code: this is my index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Run Python Script</title>
</head>
<body>
<h1>Python Script Runner</h1>
<button id="runPython">Train Model</button>
<p id="output">Output will appear here...</p>
<script>
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
document.getElementById('runPython').addEventListener('click', () => {
let pythonExecutable = path.join(__dirname, 'resources', 'python3.10');
if (!fs.existsSync(pythonExecutable)) {
pythonExecutable = path.join(process.resourcesPath, 'resources', 'python3.10');
}
const appScriptPath = path.join(__dirname, 'app.py');
const command = `"${pythonExecutable}" "${appScriptPath}"`;
exec(command, {shell: true}, (error, stdout, stderr) => {
if (error) {
document.getElementById('output').innerText = `Error: ${error.message}`;
return;
}
if (stderr) {
document.getElementById('output').innerText = `stderr: ${stderr}`;
return;
}
try {
const output = stdout.toString();
console.log('Received output from Python:', output);
document.getElementById('output').innerText = `Output: ${output}`;
} catch (e) {
console.error('Error parsing JSON:', e);
document.getElementById('output').innerText = 'Error parsing JSON output from Python script.';
}
});
});
</script>
</body>
</html>
Setup Details:
Electron Version: (specify version)
Electron Packager: Used for creating the DMG file (macOS)
The application is designed to train a model in Python, which is a critical requirement for my project. I am using electron-packager
for packaging.
Question:
I am new to Electron and am unsure why the path error occurs post-packaging but not during local development. Could someone help identify what I might be missing, or suggest what should be checked or changed in the setup?
Additional Request:
Please do not downvote this question without providing feedback. I am looking to improve the question based on your suggestions.
Divakar Yadav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.