1

I have to lanch some apt install commands inside a Python script, and this is possible using the subprocess. But the problem is that I can't see the installation progress (error, completed, didn't start, ongoing etc.). Is there a way to print the apt logs just like in bash?

  • 1
    Does this answer your question? [Constantly print Subprocess output while process is running](https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running) – Ron Serruya Jun 20 '22 at 07:49
  • Can you provide some code indicating how you are using Python for it? According to the [subprocess library](https://docs.python.org/3/library/subprocess.html), if you use subprocess.run(args, capture_output=True) maybe you get what you want. – adriaat Jun 20 '22 at 07:51
  • not really.. what i need is something that prints every line just like in cmd. capture_output looks like something i dont need.. but thank you anyway for the effort. omw to check @RonSerruya – Marco Frag Delle Monache Jun 20 '22 at 08:03
  • sadly, @RonSerruya answer doesn't solve the problem. Still, thank you very much – Marco Frag Delle Monache Jun 20 '22 at 08:18

1 Answers1

1

Check out this example. It uses iter() over stdout from Popen. Since you didn't povide a code how do you call apt get I used ping for demo but the principle is the same.

import subprocess

process = subprocess.Popen(["ping", "-c", "3", "google.com"], stdout=subprocess.PIPE, universal_newlines=True)
for line in iter(process.stdout.readline, ""):
    print(line)
process.stdout.close()
return_code = process.wait()
olegr
  • 1,999
  • 18
  • 23