0

I'm using a library called python-shell to try and send data back and forth between node and python. The code below should work but when I run it I get this error:

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: write EPIPE
at exports._errnoException (util.js:1022:11)
at Socket._writeGeneric (net.js:712:26)
at Socket._write (net.js:731:8)
at doWrite (_stream_writable.js:334:12)
at writeOrBuffer (_stream_writable.js:320:5)
at Socket.Writable.write (_stream_writable.js:247:11)
at Socket.write (net.js:658:40)
at PythonShell.send (C:\Users\user\Desktop\node py project\fourth draft\node
_modules\python-shell\index.js:205:16)
at Object.<anonymous> (C:\Users\user\Desktop\node py project\fourth draft\in
dex.js:4:9)
at Module._compile (module.js:570:32)

my index.js:

var PythonShell = require('python-shell');
var pyshell = new PythonShell('script.py');

pyshell.send(JSON.stringify([1,2,3,4,5]));//the problem function

pyshell.on('message', function (message) {
    // received a message sent from the Python script (a simple "print" statement)
    console.log(message);
});

// end the input stream and allow the process to exit
pyshell.end(function (err) {
    if (err){
        throw err;
    };

    console.log('finished');
});

my script.py:

import sys, json

#Read data from stdin
def read_in():
    lines = sys.stdin.readlines()
    # Since our input would only be having one line, parse our JSON data from that
    return json.loads(lines[0])

def main():
    #get our data as an array from read_in()
    lines = read_in()

    # Sum  of all the items in the providen array
    total_sum_inArray = 0
    for item in lines:
        total_sum_inArray += item

    #return the sum to the output stream
    print total_sum_inArray

# Start process
if __name__ == '__main__':
    main()

I think it has to do with the env path for python on win 7.

Any ideas? Library docs are located here: https://github.com/extrabacon/python-shell. Also I'm on Windows 7 Node v6.9.2 Python v3.6.1.

1 Answer 1

2

Try passing the parameter of python path to the script as below

var pyshell = new PythonShell('script.py',{pythonPath : '<path/to/python>'});

In this way you are explicitly setting the path to python executable command, regardless of any OS.

Here is reference of all the available options, that can be passed to the python-shell constructor.

Hope this fixes the issue, if it has to do anything with the Python path issues.

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

1 Comment

Thank you! Somehow missed that option, perfect answer.

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.