1

I am trying to use subprocess.run with a custom argument, that works both directly from windows cmd, and from subprocess.call, but not with subprocess.run, as suggested by Running shell command and capturing the output.

This works:

    retcode = subprocess.call(build_command_line, shell=True)

This fails:

    result = subprocess.run([build_command_line], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

and outputs

The filename, directory name, or volume label syntax is incorrect.
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files\Python37\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
  File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 551, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
PermissionError: [WinError 5] Access is denied

even though I run with administrator.

Not that it should matter, but for completeness:

build_command_line is:

'"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe" "C:\\code\\EPMD\\Kodex\\Solutions\\Kodex.All.sln" /t:REBUILD /p:Configuration=RELEASE'

How do I get it to work with subprocess.run?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • either build_command line is a string (and then you are passing a list with a single string) or it is a list (and then you are passing a list with a single element which is a list), please clarify the contents of build_ccommand line, can't get what it is from what you wrote – E.Serra Jun 22 '20 at 15:48
  • @E.Serra Edited. It is a string. So i should call with no `[]`? – Gulzar Jun 22 '20 at 15:54
  • 2
    `[build_command_line]` is wrong. Just use `build_command_line`. In Windows you can always use a command-line string. If you pass an `args` list instead of a command line, `subprocess.Popen` uses `subprocess.list2cmdline` to build the command line according to the rules used by MSVC and WinAPI `CommandLineToArgvW`. With `[build_command_line]`, the whole command line is one item in the list, so all of the quotes get escaped as literal characters. – Eryk Sun Jun 23 '20 at 02:27
  • shell=True allows you to write whatever command as a string, the usual way of ccalling it is to pass it a list with the arguments and values as elements, e.g. ['ls', '-a', '-l'], break your command into a true list and give it a go – E.Serra Jun 23 '20 at 08:19
  • @E.Serra, it's Windows, so passing a command-line string does not require `shell=True`. If the script needs to support Unix, then in that case it should use an args list. – Eryk Sun Jun 25 '20 at 01:25

1 Answers1

1

Problem was my understanding of subprocess.run

correct call:

result = subprocess.run(build_command_line, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Gulzar
  • 23,452
  • 27
  • 113
  • 201