0

I am trying to execute this shell command using python

but the problem is, it doesn't give the output even if there is a wrong or not:

This is what I have tried:

get_ns_p1 = subprocess.Popen(['kubectl', 'get', 'ns'], stdout=subprocess.PIPE)
get_ns_p2 = subprocess.Popen(["grep", "-E", "\'(^|\s)"+NAMESPACE+"($|\s)\'"], stdin=get_ns_p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

get_ns_p1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
out_ns, err = get_ns_p2.communicate()

print("output: " + out_ns)
print("error: " + err)

The output looks like:

output:

error:

but in the terminal itseld, it shows an output like this:

Error from server (AlreadyExists): namespaces "namespace-tests" already exists

how can I this error to my err variable?

Community
  • 1
  • 1

1 Answers1

0

Your code works correctly; the problem is that you are attempting to capture a string which is emitted on stderr if kubectl is at all correctly written.

However, running grep as a separate subprocess is quite inefficient and unnecessary anyway; Python itself features a much more featureful and complete regular expression engine in the re library which also lets you avoid the overhead of starting a separate process.

import re

r = re.compile(r'(?:^|\s){}(?:\s|$)'.format(NAMESPACE))

kube = subprocess.run(['kubectl', 'get', 'ns'], text=True, capture_output=True)
e = []
for line in kube.stderr.split('\n'):
    if r.search(err):
        e.append(line)
err = '\n'.join(line)

If you are confined to an older version of Python which doesn't have subprocess.run(), you have to reimplement it, poorly.

p = subprocess.Popen(['kubectl', 'get', 'ns'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_ns, err = p.communicate()
e = []
for line in err.split('\n'):
    ...

This has a number of flaws and drawbacks; you really should be thinking seriously about upgrading to Python 3.5+.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • See also https://stackoverflow.com/questions/40590192/getting-an-error-attributeerror-module-object-has-no-attribute-run-while for a better reimplementation. – tripleee Nov 29 '19 at 12:12