Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start node app from python script

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?

like image 909
user3316792 Avatar asked Mar 04 '16 05:03

user3316792


People also ask

How do you call a node JS API in Python?

const spawn = require("child_process"). spawn; const pythonProcess = spawn('python',["path/to/script.py", arg1, arg2, ...]); Then all you have to do is make sure that you import sys in your python script, and then you can access arg1 using sys. argv[1] , arg2 using sys.

How do I start a node script?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.


1 Answers

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.

like image 198
Andriy Ivaneyko Avatar answered Sep 22 '22 06:09

Andriy Ivaneyko