0

I copied and paste the codes from python-shell documentation and have been working on it for the past 2 days, can't seems to get the python-shell to work in electronJS. Unfortunately, I cannot even begin to understand the error message below:

error message

events.js:292 Uncaught Error: spawn ../pyFiles/env/Scripts/python.exe ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:267)
    at onErrorNT (internal/child_process.js:469)
    at processTicksAndRejections (internal/process/task_queues.js:84)
errnoException @ internal/errors.js:514
ChildProcess._handle.onexit @ internal/child_process.js:267
onErrorNT @ internal/child_process.js:469
processTicksAndRejections @ internal/process/task_queues.js:84

app.js

const {PythonShell} = require('python-shell')

let submitBtn = document.querySelector('#submit')
submitBtn.addEventListener("click", (e) => {
    e.preventDefault();

    let options = {
        mode: 'text',
        pythonPath: '../pyFiles/env/Scripts/python.exe',
        pythonOptions: ['-u'], // get print results in real-time
        scriptPath: '../pyFiles',
        args: ['World']
    };

    PythonShell.run('hello.py', options, function (err, results) {
        if (err) throw err;
        console.log('results: %j', results)
        console.log('Finished')
    })
    console.log('Hello from Javascript1');
});

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Payroll Report Generator</title>
  </head>
  <body>

    <div class="container">
      <h1>Payroll Report</h1>
      <p>Generate ProSoft Uploadable Excel File with a click!</p>

      <form>
        <button id="submit" class="btn btn-primary">Convert Now!</div>
      </form>

      <script src="./app.js"></script>
  
  </body>
</html>
Cadell Teng
  • 224
  • 3
  • 23
  • 1
    Are the paths correct? `ENOENT` is `No such file or directory`. It's unlikely that the `pyFiles` folder is a sibling of your current folder. – Ouroborus Dec 30 '20 at 05:24
  • 1
    first try with `/full/path/to/python` and `/full/path/to/pyFiles/script.py` – furas Dec 30 '20 at 06:50
  • @Ouroborus the file is located at where I want it to be, my path is correct too. – Cadell Teng Dec 30 '20 at 12:43
  • @furas it worked! Thanks. What I don't understand is why does a 'relative path' defaults to my C:/Users/{username}/appData/Local/Microsoft/...... instead of the folder I am in? – Cadell Teng Dec 30 '20 at 12:44
  • 1
    system may starts running code in different folder then you expect. It doesn't have to be folder with your code. It is so called `"Current Working Directory"` which you can check with `os.getcwd()`. And then relative path also search in `"Current Working Directory"`, not in folder with your code. – furas Dec 30 '20 at 12:59
  • 1
    [How do you properly determine the current script directory in Python?](https://stackoverflow.com/questions/3718657/how-do-you-properly-determine-the-current-script-directory-in-python) And you can use it with relative path and `os.path.join()` to create full path. – furas Dec 30 '20 at 13:03
  • @furas Got it. Thanks so much! – Cadell Teng Dec 30 '20 at 14:28

0 Answers0