Start node application from python script

Is it possible to run a node.js application from a python script on a raspberry pi?

On the command line, I ran sudo node myscript.js

Can I use a library like os?

+5
source share
2 answers

The first line of the file should be:

 #!/usr/bin/python 

You can invoke the command using subprocess.call :

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

Then you must set +x permissions for the executable:

 chmod +x filename.py 

Know that you are ready to work:

 ./filename.py 

Note: checkout Raspberry Pi Stack Exchange , you can find a lot of useful information here.

+5
source

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

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

You will probably encounter a FileNotFoundError when trying to execute a command using sudo . If you do, you can try:

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

In the Python documentation, be VERY careful using the shell=True parameter, as this can be a problem if you allow any arbitrary user to enter subprocess.call() .

+2
source

Source: https://habr.com/ru/post/1244401/


All Articles