简体   繁体   中英

Running python-shell from ElectronJS script

I'm creating a simple desktop app with Electron JS and Python. I tried a tutorial which indicates that it's more simple to communicate with Python back end with python-shell module. I cannot run the app without error related to that module.

Firstly I started with following code:

var pyshell =  require('python-shell');

pyshell.run('hello.py',  function  (err, results)  {
 if  (err)  throw err;
 console.log('hello.py finished.');
 console.log('results', results);
});

However it resulted in error saying that " pyshell.run is not a function ". So I tried replacing first line with let {PythonShell} = require('python-shell') . Also, tried const instead of let . It doesn't work, and pop ups error message "Uncaught Exception: Error: spawn py ENOENT ". None of examples provided by Internet works for me... Where is the problem?

First of all you can assign a const like ;

const pyshell = require('python-shell').PythonShell
// or
const {PythonShell} = require('python-shell')

and your code does not work without options because command needs a directory, you should use python-shell like;

let {PythonShell} = require('python-shell');


var options = {
    scriptPath : path.join(__dirname, '/YOUR PYTHON DIRECTORY'),
    args : [],
    //mode: "json"
};

let pyshell = new PythonShell('test.py', options);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM