4

i've got a script, that contains a list - the list is just some args that I want to pass to subprocess.run like this

commands = ["bash command 1", "bash command 2",..]

here is my code

commands = ["bash command 1", "bash command 2",..]
process = subprocess.run([commands], stdout = subprocess.PIPE, shell = True)

how can I pass a list to my subprocess.run?

this is the Traceback

Traceback (most recent call last):
  File "./retesting.py", line 18, in <module>
    process = subprocess.run([commands], stdout = subprocess.PIPE, shell = True)
  File "/usr/lib/python3.5/subprocess.py", line 383, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1221, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert 'list' object to str implicitly

I Have no idea what I'm doing wrong and I tried all sorts of things, so I'd appreciate really any help

8
  • Why do you define your commands as a set(using {}) when you need a list (using [])? Sep 17, 2018 at 9:34
  • sorry that was actually a mistake i had while typing out the question, edited my question
    – skdadle
    Sep 17, 2018 at 9:36
  • 2
    Simply drop the [] from .run([commands],. Sep 17, 2018 at 9:36
  • @Rakesh thanks for the feedback, I changed my code to what you suggested and i dont get any errors, only now it seems to pass the first argument to bash and ignores the rest. what could i be doing wrong? thanks a lot!
    – skdadle
    Sep 17, 2018 at 9:36
  • 1
    @skdadle Do not use shell=True. It changes how the command list is interpreted. Sep 17, 2018 at 9:41

1 Answer 1

9

Before using shell=True you have to understand what it does. Take the documentation for Popen. It states:

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

There is no way to execute a sequence of commands in one go, what you are doing is executing the first command and all other commands as passed when spawning the shell as options to that shell.

You want to do this instead:

for command in commands:
    subprocess.run(command, shell=True)

Alternatively: you want to execute the sequence of commands as a single script:

subprocess.run(';'.join(commands), shell=True)

This said: if the commands inside command are only executables you should really avoid using shell=True and use shlex.split to provide the parsed argument list. If you need to specify the directory where the command should be executed you can use the cwd argument to Popen (or any similar function).

In your case you want something like:

import os, subprocess

subprocess.run(['app', '--email', 'some-email', '--password', 'somepassword'], cwd=os.path.expanduser('~/app-cli'))
5
  • thanks for the answer!, but now i get an invalid syntax error in my first string of my list, but when i pass it normally as one argument like this subprocess.run("the exact same string like my first one in the list", shell = true) it works, any ideas?
    – skdadle
    Sep 17, 2018 at 9:56
  • in your answer u show me how to pass one argument to subprocess.run, cant I pass a sequence/list of arguments that it executes one after the other?
    – skdadle
    Sep 17, 2018 at 10:02
  • @skdadle subprocess.run always execute one thing. This thing can either be an executable (when shell=False), in which case the argument is a list of strings that specifies the command line, or it can execute a single shell script (when shell=True). A shell script can be composed of multiple shell commands. In UNIX style shells you can separate multiple commands using ; (or newlines). As I said: I think you should avoid shell=True or executing multiple commands if you can. If you have more issue please edit your question to include the errors you get. Sep 17, 2018 at 14:38
  • hi, thanks for your answer, i tried your solution - with (';'.join(commands), shell = True) but i got an invalid syntax error, I'm confused because each command is enclosed in double quotes, separated by a comma, and i thought we should write our command exactly like how we would write it in shell.. i cant use the second answer you suggested because i have a larger number of commands and i don't want to type each one in separately , that's also why i wanted to use a list in the first place, also i need shell = true because im not running executables. Thanks a lot!
    – skdadle
    Sep 18, 2018 at 8:47
  • @skdadle Without providing the exact list of commands you want to run we have to way of knowing why you get a syntax error. If you want better answers you should try to provide as much information as possible in your question. If you give generic information you will receive generic answers. Sometimes that's enough, other times they are suboptimal. Sep 18, 2018 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.