pip-update script

39 views
Skip to first unread message

Jacob Yates

unread,
Jun 17, 2015, 10:26:29 AM6/17/15
to pypa...@googlegroups.com
I wrote a very basic script to update all the installed modules through pip which I think is very useful as I had to update the ones I used manually which got very tiresome after my modules list had became very large.

#!/bin/bash
sudo pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 sudo pip install -U


I added this to my /usr/bin   as pip-update which has really made it easy, I'm sure the same functionality can be applied to Windows Batch, Or even wrote in python but I just made a quick dirty script.

Jacob Yates

unread,
Jun 17, 2015, 10:28:53 AM6/17/15
to pypa...@googlegroups.com

Here is a screenshot of the script in progress.

Jacob Yates

unread,
Jun 17, 2015, 12:45:32 PM6/17/15
to pypa...@googlegroups.com

I'm attempting to work this into python but coming across an issue

import os

update_list = []
p = os.popen('sudo pip freeze --local',"r")
while 1:
    line = p.readline()
    if not line: break
    update_list.append(line.rstrip('\n').split('=')[0])
for uplist in update_list:
    os.system('sudo pip install -U %s') % uplist

[b]
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 223, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 268, in run
    wheel_cache
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 268, in populate_requirement_set
    wheel_cache=wheel_cache
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py", line 207, in from_line
    wheel_cache=wheel_cache)
  File "/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py", line 66, in __init__
    req = pkg_resources.Requirement.parse(req)
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2960, in parse
    reqs = list(parse_requirements(s))
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2891, in parse_requirements
    raise ValueError("Missing distribution spec", line)
ValueError: ('Missing distribution spec', '%s')[/b]

Jacob Yates

unread,
Jun 17, 2015, 1:19:34 PM6/17/15
to pypa...@googlegroups.com
Okay here is a working example in python


import os

update_list = []
p = os.popen('sudo pip freeze --local',"r")
while 1:
    line = p.readline()
    if not line: break
    update_list.append(line.rstrip('\n').split('=')[0])
counter = 0
while counter < len(update_list):
    os.system('sudo pip install -U "' + update_list[counter] + '"')
    counter += 1

Jason R. Coombs

unread,
Jun 17, 2015, 2:06:48 PM6/17/15
to Jacob Yates, pypa...@googlegroups.com
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

Jacob Yates

unread,
Jun 17, 2015, 2:12:46 PM6/17/15
to pypa...@googlegroups.com, blm...@gmail.com

Thank you very much, and thank you for showing me a more modern example!!!, Do you think that this will ever be implemented into pip as a update switch?
Reply all
Reply to author
Forward
0 new messages