1

I have a problem launching a npm command via a python script.

from Run npm commands using Python subprocess I found that the following should work:

subprocess.check_call('start npm run features:chrome:server', shell=True)

and it does (!).

From documentation (https://docs.python.org/3/library/subprocess.html), I read that subprocess.check_call is equivalent to run(..., check=True)

As I previousely used subprocess.run to launch an external application (newman) with success, I tried the following:

subprocess.run("start npm run features:chrome:server", check=True)

but it ends up with an error:

Traceback (most recent call last):
  File "test_differentiel.py", line 73, in <module>
    subprocess.run("start npm run features:chrome:server")
  File "C:\Users\a.joly\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\a.joly\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:\Users\a.joly\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable

Any idea of why I can't use subprocess.run ? (nor check_output, by the way ...)

M Dennis
  • 177
  • 7
A.Joly
  • 2,317
  • 2
  • 20
  • 25
  • From the error message, you forget to add `shell=True` in `subprocess.run()`, hence the full string `'start npm run features:chrome:server'` is assumed to be the target executable. And I think it's safe to assume that that file doesn't exist ... – dhke Sep 13 '17 at 13:11
  • good point, I didn't set shell=True, let's try it ... ! great ! it works ! :) thank you. – A.Joly Sep 13 '17 at 13:16
  • 1
    `shell=True` splits the argument string (by passing it to the shell). `shell=False` tries to a run an executable named `'start npm run features:chrome:server'` (the full string with spaces, not just `start.exe`). – dhke Sep 13 '17 at 13:19

1 Answers1

1

The correct execution is subprocess.run(['start', 'npm', 'run', 'features:chrome:server'], check=True)

subprocess.check_call('start npm run features:chrome:server', shell=True) works because you invoked the shell. Further information you could find here.

subprocess.run("start npm run features:chrome:server", check=True, shell=True) should also work.

M Dennis
  • 177
  • 7
  • This doesn't work actually, shell=True is really the point ... though the syntax may be more pythonic (?) – A.Joly Sep 13 '17 at 13:24