# \[Python\] - subprocess issue

**URL:** <https://community.unix.com/t/python-subprocess-issue/376295>\
**Category:** Programming\
**Created:** [September 18, 2019, 10:07am UTC](https://community.unix.com/t/python-subprocess-issue/376295 "2019-09-18T10:07:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scj2012](https://community.unix.com/letter_avatar/scj2012/32/5_5575768a8748004e209b776fc1b2916d.png) [@scj2012](https://community.unix.com/u/scj2012)\
**Post date:** [September 18, 2019, 10:07am UTC](https://community.unix.com/t/python-subprocess-issue/376295/1 "2019-09-18T10:07:52Z")

</div>

So I have this basic script, see below

```nohighlight
import subprocess
import shlex

command = "gcloud projects list"

subprocess.check_output(shlex.split(command))
subprocess.check_call(shlex.split(command))

```

The `subprocess.check_call(shlex.split(command))` actually return what I expect. It returns the result of `gcloud` projects list. However `subprocess.check_output` doesn't, in fact it returns nothing.

Can explain this?

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 18, 2019, 10:23am UTC](https://community.unix.com/t/python-subprocess-issue/376295/2 "2019-09-18T10:23:23Z")

</div>

My understanding is the operation of the two are different:

Documentation -

> [@](#):
>
> check\_call() returns as soon as /bin/sh process exits, it does not wait for child processes to complete
> 
> check\_output() waits until all output is read. When (and if) check\_output inherits the stdin pipe then check\_output() will wait until it exits, i.e., until the child process the inherited pipe. In other words it waits for the child process to end.

Try getting get rid of the shlex,

```plaintext
subprocess.check_output( ["command" "parameter_1"])

```

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [September 18, 2019, 10:56am UTC](https://community.unix.com/t/python-subprocess-issue/376295/3 "2019-09-18T10:56:22Z")

</div>

It is also possible that the command that you are running is sending the output to `stderr` instead of `stdout`

Try this instead:-

```nohighlight
subprocess.check_output(shlex.split(command), stderr=subprocess.STDOUT)

```

---

<div class="post-metadata">

**Author:** ![scj2012](https://community.unix.com/letter_avatar/scj2012/32/5_5575768a8748004e209b776fc1b2916d.png) [@scj2012](https://community.unix.com/u/scj2012)\
**Post date:** [September 18, 2019, 10:59am UTC](https://community.unix.com/t/python-subprocess-issue/376295/4 "2019-09-18T10:59:54Z")

</div>

> [@jim mcnamara](#):
>
> My understanding is the operation of the two are different:
> 
> Documentation -
> 
> Try getting get rid of the shlex,
> 
> ```plaintext
> subprocess.check_output( ["command" "parameter_1"])
> 
> ```

That's not going to work. I need the shlex and even if I didn't have it, it still wouldn't work.

---

<div class="post-metadata">

**Author:** ![scj2012](https://community.unix.com/letter_avatar/scj2012/32/5_5575768a8748004e209b776fc1b2916d.png) [@scj2012](https://community.unix.com/u/scj2012)\
**Post date:** [September 18, 2019, 12:13pm UTC](https://community.unix.com/t/python-subprocess-issue/376295/5 "2019-09-18T12:13:28Z")

</div>

> [@yoda](#):
>
> It is also possible that the command that you are running is sending the output to `stderr` instead of `stdout`
> 
> Try this instead:-
> 
> ```plaintext
> subprocess.check_output(shlex.split(command), stderr=subprocess.STDOUT)
> 
> ```

That doesn't work etiher. Also the exact same command works with subprocess.check\_call. What's weird is the subprocess.check\_output works in the python shell but not from the python script.

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [September 18, 2019, 1:11pm UTC](https://community.unix.com/t/python-subprocess-issue/376295/6 "2019-09-18T13:11:04Z")

</div>

To see the output from program, you have to `print` the output. I believe `stdout` is used internally for this function:-

```nohighlight
print subprocess.check_output(shlex.split(command))

```

---

<div class="post-metadata">

**Author:** ![scj2012](https://community.unix.com/letter_avatar/scj2012/32/5_5575768a8748004e209b776fc1b2916d.png) [@scj2012](https://community.unix.com/u/scj2012)\
**Post date:** [September 18, 2019, 1:24pm UTC](https://community.unix.com/t/python-subprocess-issue/376295/7 "2019-09-18T13:24:48Z")

</div>

> [@yoda](#):
>
> To see the output from program, you have to `print` the output. I believe `stdout` is used internally for this function:-
> 
> ```plaintext
> print subprocess.check_output(shlex.split(command))
> 
> ```

That does work. Why does it work from the python shell without print?

Also I looked at [17.1. subprocess � Subprocess management � Python 2.7.16 documentation](https://docs.python.org/2/library/subprocess.html) and it doesn't mention anything about having to do a print.
