electron-react-boilerplate / electron-react-boilerplate

A Foundation for Scalable Cross-Platform Apps

Home Page:https://electron-react-boilerplate.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Run python script within a component using child-process

GimliAlban opened this issue · comments

I am new to electron and react and I'm trying to run a python script using child-process.

I want to trigger the python script whitin the componentDidMount method of my class:

componentDidMount(): void {
    var pyscript  = spawn("python", ["../scripts/hello.py"])
    // python.stdout.on('data', function(data){
    //     console.log("data: ", data.toString('utf8'));
    // });
}

First problem that happend was Can't resolve 'child_process', I looked online and found that I needed to add the following to my package.json:

"browser": {
    "child_process": false
  }

It fixed the error but now I have this error and I can't find how to fix it:

TypeError: (0 , child_process__WEBPACK_IMPORTED_MODULE_2__.spawn) is not a function

I also tried to used python-shell but it used child-process and I was having many other errors.

What does your python script look like? We found issues with it working unless you wrap it in a if name == "main":

block. Running the script in dev should work this way, and when you're packaged, make sure you've also packaged your python script into an executable (we use pyinstaller as <pyinstaller --onefile python.py>. Make sure to account for file path changes if necessary in packaged.