3

I have written following code to spawn a python process. I am able to launch other processes but not python. I tried reinstalling python and node but still no luck. I am able to run python from command line. Please help.

const spawn = require("child_process").spawn;
var process = spawn('python',[ 'D:/python_script.py']);

var responseData = "";

process.stdout.setEncoding('utf-8');
process.stdout.on('data', function (data){
    responseData += data.toString();
});
process.stdout.on('end',function(data){
    console.log(JSON.stringify(responseData));
});

Using node 64 bit v8.2.1

Python script which I am using:

if __name__ == '__main__':
    import sys
    print("Hello")
    f = open('D:/myfile.txt', 'w')
    f.write('hi there\n')  # python will convert \n to os.linesep
    f.close()
    sys.stdout.flush()

Even just spawn('python'); is not launching python window

I have also tried giving absolute path of python.exe.

4
  • are you getting any errors? Commented Jul 26, 2017 at 6:27
  • No errors. Just nothing is happening. Commented Jul 26, 2017 at 6:50
  • can you post the python script as well? Commented Jul 26, 2017 at 8:24
  • Maybe you can use python-shell? Commented Jul 26, 2017 at 20:01

1 Answer 1

1

change

console.log(JSON.stringify(responseData));

to

console.log(responseData);

and add

process.stderr.on('data', function (data){
    responseData += data.toString();
});

below

process.stdout.on('data', function (data){
    responseData += data.toString();
});
Sign up to request clarification or add additional context in comments.

Comments

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.