Temple Statue Fog Rolling In Headfirst Marin Headlands Cementerio de Trenes de Uyuni Fish Market Machhapuchhre Annapurna South Vocano Scarred Mountains Golden Gate Fall Colors Hong Kong from the Peak Sunset From Moro Rock White Mountains Sunset Alamo Square Boudhanath Stupa Half Dome at Sunset Chinchero at Sunset Sunset from Marshall Beach, SF Sand Dunes Old Man in Siding Monument Valley Zabriskie Point Photographers Cumhuriyet Anıtı SF Cable Car Route Twilight Fog Sunset Annapurna South in the Early Morning Half Dome at Sunset Bodie Mardi Himal Trail The Blue Mosque The Golden Gate at Night Sheepherders and Annapurna South Llamas, Alpacas and Mutants Buildings and a Bridge Cropped Night Cats Market Flags Rooster Fights at Forest Camp Rolling Fog Bay Area Sunrise Crosswalk Prepping to FIght Yosemite Fall Leaves Eastern Sierras Under Shadow Phewa Boats Very rare deserted street Prayer Flags Mardi Himal Trail Snow and Shadows Bluebird Skies Zabriskie Point The Buttermilks Pisac Hillside Green River Canyons at Sunrise Annapurna South Under the Stars The Danube at Night Stars Over Annapurna South Resting Grafitti

Checking for Python Package Updates at PyPI

April 01, 2012 —

I frequently find myself wondering if a bug in a Python package has been fixed and whether there is an upgrade for that package that might fix the bug. So I find that I end up running pip freeze and then having to compare the package versions to those on PyPI manually. Well, anytime you say "run X manually", you're being a chump.

I just saw down and wrote a script to get the list of currently installed packages in the current environment (so it works with virtualenv). Then it checks to see what the latest version of the package is on PyPI and prints out the status. If you work with Python and packages, this is awesomesauce.

I added the script to my dotfiles on github.

#!/usr/bin/env python
import xmlrpclib
import pip
import argparse
from pkg_resources import parse_version
def version_number_compare(version1, version2):
return cmp(parse_version(version1), parse_version(version2))
def print_status(package, message):
package_str = '{package.project_name} {package.version}'.format(package=package)
print '{package:40} {message}'.format(package=package_str, message=message)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-a', '--all', dest='all', action='store_true', default=False)
parser.add_argument('-m', '--mirror', dest='mirror', default='http://pypi.python.org/pypi')
args = parser.parse_args()
if not args:
exit(-1)
pypi = xmlrpclib.ServerProxy(args.mirror)
for dist in pip.get_installed_distributions():
available = pypi.package_releases(dist.project_name)
if not available:
# Try the capitalized package name
available = pypi.package_releases(dist.project_name.capitalize())
upgrade_available = True
if not available:
print_status(dist, 'no releases at pypi')
continue
comparison = version_number_compare(available[0], dist.version)
if comparison == 0:
if not args.all:
continue
print_status(dist, 'up to date')
elif comparison < 0:
print_status(dist, 'older version on pypi')
else:
print_status(dist, '%s available' % available[0])
view raw pyupgrades.py hosted with ❤ by GitHub

Assuming the script is at ~/bin/pyupgrades.py, you can run ...

read more

Python virtualenvwrapper and Logging

January 20, 2011 —

I ran into this issue, and couldn't find a resolution anywhere online. Well, I figured it out, and thought that I'd put it online for others to find.

The issue was that I got exception tracebacks when sourcing the /usr/local/bin/virtualenvwrapper.sh script upon bash initialization. However, the exception happens while Python is running it's atexit handlers. So the stack was only 3 levels deep. I'm running virtualenv and virtualenvwrapper on my Ubuntu 9.04 server.

Here's the output of the exception that I got:

[streeter@mail]:~$ source /usr/local/bin/virtualenvwrapper.sh
Traceback (most recent call last):
File "/usr/lib/python2.6/logging/handlers.py", line 72, in emit
self.doRollover()
File "/usr/lib/python2.6/logging/handlers.py", line 129, in doRollover
os.rename(self.baseFilename, dfn)
OSError: [Errno 13] Permission denied
Traceback (most recent call last):
File "/usr/lib/python2.6/logging/handlers.py", line 71, in emit
if self.shouldRollover(record):
File "/usr/lib/python2.6/logging/handlers.py", line 145, in shouldRollover
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
ValueError: I/O operation on closed file
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
h.flush()
File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
self.stream.flush()
ValueError: I/O operation on closed file
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
h.flush()
File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
self.stream.flush()
ValueError: I/O operation on closed file
[streeter@mail]:~$

read more