1

I'm using NodeJS, and a Python script.

I need to get results from my python script, for that, I use Python-Shell. See the documentation at this link :

github.com/extrabacon/python-shell

I can get prints by using pythonShell.on and pythonShell.end.

The problem is that I can't send args with this method

Then I use pythonShell.run!

I can send args but it doesn't return prints while it should ....

Can you help to get my prints ?

You can see my short code below, it's a simple code, just to make it work.

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

app.post('/index/generator/',urlencodedParser, function (req,res){
  var options = {   
    mode: 'JSON',
    pythonOptions: ['-u'],
    scriptPath: './generator',
    args: ['hello','bye']
  };

  pythonShell.run('generator.py', options, function (err, results) {    
    if (err) throw err;
    console.log("Message %j ",results);
  });
})

Here the python code :

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys

print(sys.argv[1]+' '+sys.argv[2])

1 Answer 1

1

You can use child_process,

  1. make python file executable

    chmod +x generator.py

  2. spawn child process

```

const { spawn } = require('child_process');
const ls = spawn('./generator.py', ['hello', 'world']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

```

then use process.send to communicate with parent process

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your answer, it works, so I will use your method if nobody can find the solution of pythonShell.run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.