Hi Jacob. Good work getting through it. Here’s how I’d write the same thing using more modern constructs:
import subprocess
proc = subprocess.Popen([’sudo', 'pip', 'freeze', '--local'], stdout=subprocess.PIPE)
update_list = [line.decode().rstrip('\n').split('=')[0] for line in proc.stdout]
subprocess.Popen(['sudo', 'pip', 'install', '-U’] + update_list).wait()
This technique does a couple of things.
First, it uses the subprocess module in favor of the deprecated os.popen and os.system.
Second, it builds the update_list using a list comprehension (decoding the input for compatibility on Python 3).
Finally, for the update command, it takes advantage of the fact that pip allows multiple requirements to be set on a single invocation of pip, thus letting it resolve the updates. Because it uses the subprocess module, the individual items are automatically quoted as necessary.
Hope that helps, and glad for your help.
Regards,
Jason