start node app from python script

前端 未结 2 722
梦如初夏
梦如初夏 2021-01-02 23:06

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

2条回答
  •  迷失自我
    2021-01-02 23:14

    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().

提交回复
热议问题