Search code examples
pythoncommand-linesubprocessos.system

Execute npm Script in a different Folder with python


Can someone tell me how I can do "npm run start" in any folder using a Python script. But please with the "os" operator and not "subprocess".

EDIT: I need a python script, that goes to a specific folder and then executes "npm run start". How can I do that?


Solution

  • You can "change dir" to run code in selected folder

    os.chdir("path/to/folder")
    os.system("npm run start") 
    

    or you can use ; or && to do it in os.system

    os.system("cd path/to/folder ; npm run start") 
    
    os.system("cd path/to/folder && npm run start") 
    

    or use cwd in subprocess to set Current Working Directory

    subprocess.run("npm run start", shell=True, cwd="path/to/folder")
    
    subprocess.run(["npm", "run", "start"], cwd="path/to/folder")
    

    and similar way with other methods in module subprocess