Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!