Ubuntu Python: raise an exception, import 190 modules

Imagine my surprise, while writing my first PEP 302 compliant import hook this afternoon, to carefully watch “sys.modules” for the results of my import but see it suddenly grow by nearly two hundred modules! What on earth had I done wrong? Some quick experiments revealed that my only sin was having the temerity to raise an exception. Let's try raising a simple NameError:

>>> import sys
>>> len(sys.modules)
35
>>> foo
...
NameError: name 'foo' is not defined
>>> len(sys.modules)
225

That's 190 extra modules — merely importing them takes around 60 ms on my laptop! Where are they all coming from? And how could an exception cause so many imports, including such illustrious modules as “email”, “mimetools”, and “xml”?

After reading Ubuntu's “sitecustomize.py” file and all of its consequences, the situation became clear. Their apport crash-reporting subsystem instruments Python with an exception hook that, when invoked, discovers that my system says “enabled=0” in my “/etc/default/apport” file and so it undertakes no special crash logging. But, on the way to loading the routine that performs this simple check, it performs two quite flagrant and unnecessary imports, pulling in both “apt” (that brings with it 83 packages) and “apport” (an additional 107 packages).

The solution? I have removed the “python-apport” package, along with the “ubuntuone-client” suite that depends on it. After the uninstall, exceptions are — wonderfully enough — not causing a single import of a new module! Now, finally, I can continue writing my import hook in peace.

Posted in Computing, Python | 35 Comments »

Opening tabs remotely in Google Chrome

Now that I use Google Chrome almost exclusively, I miss the fact that a running Firefox instance could be controlled from the command line so that Emacs could call for a new tab when I clicked on a URL. It would run a command something like this:

firefox -remote 'openURL(http://example.com/, new-tab)'

But after a few months of manually cutting and pasting URLs into Chrome — which wasn't actually that bad, since the address bar in Chrome is such a convenient and large target — I decided that I needed a real solution. After not finding anything like a -remote option, I discovered that Chrome can at least be run with a debugging port open:

google-chrome --remote-shell-port=9222

The protocol that Chrome speaks is primitive enough that it was quick work to implement a small client in Python. Rather than merely cutting and pasting its code here on my blog, or even be satisfied with making it available on bitbucket, I decided to place the code inside of a new Python package and make it generally available on PyPI as chrome_remote_shell.

Thanks to this simple package, a four-line program (not counting the shebang and comment) is now all that I need to ask Google Chrome to open a new tab:

#!/usr/bin/env python
# Name this file "google-chrome-open-url"
import sys
import chrome_remote_shell
shell = chrome_remote_shell.open()
shell.open_url(sys.argv[-1])

To teach Emacs to start using Google Chrome when I clicked on a link, I only needed to supply it with two new settings:

(setq browse-url-browser-function
      'browse-url-generic)
(setq browse-url-generic-program
      "google-chrome-open-url")  

And now everything works. I hope that these notes prove useful to someone else. Enjoy!

Posted in Computing, Emacs, Python | 6 Comments »

Leaving Python Magazine

It was with regret that I tendered my resignation yesterday as the Editor-in-Chief of Python Magazine. While the publisher will keep producing the magazine by distributing PDFs on the web site, the transition to the new format has dragged on long enough — both for both myself and our customers — that I have run out of enthusiasm. My last responsibility will be to shepherd the February and March issues through the publishing process and safely on to the PDF readers of our subscribers.

I hope that the authors featured in the October issue will forgive me for not writing my usual blog post last year touting their achievements; I had just received the sad news that the publisher could no longer afford the rising costs of printing and shipping Python Magazine, and I did not want to further advertise the magazine until its fate was certain one way or the other.

I have by no means been a perfect editor. In particular, the publisher hoped that I would get the magazine — which was running eight weeks late — back on schedule. Instead, my bumbling first month as editor made the magazine an additional week late, and by the time I hit my stride in May it was another week behind. Although the schedule then stabilized at a steady ten weeks late, I never did manage to start reeling the fish back in. The only metric, I suppose, which I can really claim to my credit is that I oversaw a nineteen-fold increase in the number of em-dashes in the magazine — 247 appeared over the course of 2009, up from only 13 the year before!

I should express thanks to my co-workers: Arbi, Emanuela, and Cathleen are smart, helpful, and professional, and were patient with me as I learned the ropes. Doug Hellmann gave me ample training as he handed over the reins, and also supported the magazine later as an acquisitions editor. Several associate editors performed solid reviews of incoming articles. And, of course, the greatest privilege of being Editor-in-Chief was to help such a wide array of voices from the Python community find their way into print — from Steve Holden, the illustrious chair of the Python Software Foundation, to young Meran Cambpell-Hood, an eleven-year-old from New Zealand who described using Python for the first time to process data for her science fair project.

Which reminds me: the authors from the October issue never got their moment in the spotlight! The article by Meran Campbell-Hood about her science fair project was the most fun to edit, but every single article was interesting and taught me something. Steve Holden interviewed James Tauber about the secrets of a successful Python start-up; Yusdi Santoso finished his two-part series on the Python program he wrote to produce the PDF for the beautiful EuroPython brochure last year; the original editor of Python Magazine, Brian Jones, returned to talk about why he now tends to choose Django for web projects rather than PHP; and Joe Amenta introduced his "3to2" project, which will help Python programmers support their old Python 2 users while still moving ahead with the transition to Python 3. Finally, Greg Newman explained how to turn Emacs into a powerful Python IDE, and Steve Holden and myself rounded out the issue with our usual editorializing.

With many of the rest of you, I am eager to see the debut of the new Python Magazine web site. And I look forward to seeing everyone at PyCon 2010 in less than three weeks! While I will not have the joy that I did last year of walking the halls of PyCon as a newly-minted Editor-in-Chief, able to make dreams come true and grant the fame and fortune of being a published author, I will at least enjoy being a developer among developers in the best programming language community on Earth!

Posted in Python | 14 Comments »