-1

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?

  • what did you try? Did you get error message? Show it in question. – furas Jun 16 '21 at 07:12
  • normally in system variable `PATH` you should add full path to folder with `npm` (I will repeat: `folder`, not program `npm`) and then you can run in any folder, in any language. – furas Jun 16 '21 at 07:13
  • I need a python script, that goes to a specific folder and then executes "npm run start". How can I do that? – RNS Capital Jun 16 '21 at 07:31
  • did you try `os.chdir(...)` before `os.system()` ? Or system command `cd ... && npm ...` or `cd ... ; npm ...` ? – furas Jun 16 '21 at 08:02
  • Thanks for that, got it with os.chdir(...) – RNS Capital Jun 16 '21 at 08:59

1 Answers1

1

You can run code in selecte folder

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

or

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

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

or

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 subprocess

furas
  • 134,197
  • 12
  • 106
  • 148