10

Is it possible to start a node.js app from within a python script on a raspberry pi?

On the command line I run sudo node myscript.js

could I use a library like os?

asked Mar 4, 2016 at 5:35
1

2 Answers 2

16

The first line of file shall be:

#!/usr/bin/python

You can call command with subprocess.call:

from subprocess import call
# Note that you have to specify path to script
call(["node", "path_to_script.js"]) 

Then you have to set +x permissions for file to be executable:

chmod +x filename.py

Know you are ready to go:

./filename.py 

Note: checkout Raspberry Pi Stack Exchange, you can find a lot of use full info there.

answered Mar 4, 2016 at 6:02
Sign up to request clarification or add additional context in comments.

2 Comments

How can I get the output from the command in my python script?
Figured it out. use p = subprocess.check_output(["command", "args"]) instead and decode the output with p.decode("utf-8")
4

As Selcuk mentioned in his comment, use the subprocess module:

#! /usr/bin/env python
import subprocess
subprocess.call('sudo node myscript.js')

It's very likely that you'll encounter a FileNotFoundError when trying to run your command with sudo. If you do, you can try:

#! /usr/bin/env python
import subprocess
subprocess.call('sudo node myscript.js', shell=True)

Per the Python documentation, be VERY careful about using the shell=True parameter as this could be a problem if you allow any arbitrary user input to be passed to subprocess.call().

answered Mar 4, 2016 at 5:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.