Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
How to execute a python script via javascript file in an electronjs app?

I am trying to call and execute a python script via the following javascript code in an electronjs app

function getProgramstatus(){
    
    const { exec } = require('child_process');

    exec('python python\\getProgramstatus.py', (error, stdout, stderr) => {
    });
    
    let jsonData = require('.\\js\\programstatus.json');
    let itemData = JSON.stringify(jsonData);
    let programstatus = JSON.parse(itemData);
    
    sessionStorage.setItem('programstatus check', programstatus.status);
}

The goal of the python script is to write a value at a json file.

data1={
    "status": "12",
    "time" : time.ctime(start)
}
json_object = json.dumps( data1, indent = 4)

with open('..\js\programstatus.json', 'w') as outfile:
    outfile.write(json_object)


#print("2")
sys.exit("2")

When i execute the python script via terminal it writes at json file, but when i call it from javascript it doen't write at json file. Any ideas;

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try outputting the response for hints, like this:

function getProgramstatus(){
    
    const { exec } = require('child_process');

    exec('python python\\getProgramstatus.py', (error, stdout, stderr) => {
        if (error) {
            console.error(`exec error: ${error}`);
            return;
        }
        console.log(`stdout: ${stdout}`);
        console.error(`stderr: ${stderr}`);
    });
    
    let jsonData = require('.\\js\\programstatus.json');
    let itemData = JSON.stringify(jsonData);
    let programstatus = JSON.parse(itemData);
    
    sessionStorage.setItem('programstatus check', programstatus.status);
}

And please share the output.

Also the code execution continues while getProgramstatus.py is still running, you want to use execSync / move the code below exec inside the callback or create a promise, so that let jsonData = require('.\\js\\programstatus.json'); gets executed when the python script has finished.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found the answer, why the json file was not written when the python script was called by the application and not by me at the terminal.

The problem is the folder/file location. It should be relative to the main.js of the electron app.

So I changed the following:

with open('..\js\programstatus.json', 'w') as outfile:
    outfile.write(json_object)

To: with open('.\js\programstatus.json', 'w') as outfile: outfile.write(json_object)

and work fine.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda