2

i want to run a python script via nodejs with python-shell to simply turn a led via my raspberry pi on. The script works pretty well, but i cant run it via the nodejs

my NodeJS:

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

PythonShell.run('ledtest.py', function (err) {
        if (err) throw err;
        console.log('finished');
});

my PythonScript:

import serial
import time

s = serial.Serial('/dev/ttyACM0', 9600)
s.close()
s.open()
time.sleep(5)


s.write("1")
time.sleep(2)
s.write("0")

error

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

    Error: spawn python ENOENT
        at exports._errnoException (util.js:953:11)
        at Process.ChildProcess._handle.onexit 
(internal/child_process.js:182:32)
        at onErrorNT (internal/child_process.js:348:16)
        at _combinedTickCallback (internal/process/next_tick.js:74:11)
        at process._tickCallback (internal/process/next_tick.js:98:9)
        at Function.Module.runMain (module.js:577:11)
        at startup (node.js:160:18)
        at node.js:449:3

thanks for suggestions

5
  • If you are going to run python code, why not use Flask? Commented May 23, 2016 at 13:12
  • my python architecture is 32 bit and nodejs is arm. maybe this could be the course? Commented May 23, 2016 at 14:15
  • Python comes installed on most raspberry pi OS's so I highly doubt you have 32bit installed since that would fail to even install since the Pi has an ARM processor Commented May 23, 2016 at 14:45
  • @Domagalla: Are you running the Node.js script on the Raspberry Pi? I am unable to reproduce your problem. I am using a Raspberry Pi with Raspbian 7.8, node v0.10.41, and python-shell 0.4.0. I also have a USB-to-Serial converter so that I can run your script. My only change is to use /dev/ttyUSB0. Commented May 23, 2016 at 16:53
  • yes i am running on raspberry pi but using ArchLinux. the usb port is the right one to me as well. now i elude the problem while directly communicate with the gpios via nodejs Commented May 23, 2016 at 19:29

1 Answer 1

1

You need to provide some options like the python path and directory containing your script. I would write your NodeJS script like:

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

const options = {
    mode: 'text',
    pythonPath: '/path/to/python',
    pythonOptions: ['-u'],
    scriptPath: '/directory/containing/python/script',
  };

PythonShell.run('ledtest.py', function (err) {
        if (err) throw err;
        console.log('finished');
});

You can get the python path from your Python3 shell like:

import sys
print(sys.executable)

More information on python-shell options can be found here

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.