Search code examples
javascriptpythonnpmelectrondesktop-application

npm python-shell not working with electron


I am trying to link a python script with Electron app using the npm's python-shell package.

The condition is to execute the script whenever I click on a button. So, suppose my directory contains:

main.js (electron file)
index.js (my script)
index.html
main.py

In index.js, I write the following code to import python-shell and execute the main.py script:

function get_input() {
    var python = require("python-shell")
    var path = require("path")

    var options = {
        scriptPath: path.join(__dirname, './python/')
    }

    var weather = new python('main.py', options);

    weather.on('message', function (message) {
        console.log(message);
    })
}

In the index.html, I have created a button tag to run this function on click:

<button onclick="get_input()">Go!</button>

In the main.py, all I do for now is print "hello". Now when I run, npm start I recieve the following error:

enter image description here

Does anyone know why this is not working? I saw many articles on Electron, they all do the same thing and it works. If there is something that I am missing, please let me know.

Thank you.


Solution

  • Change var python = require("python-shell")

    to

    const {PythonShell} = require("python-shell");