Python Programming, news on the Voidspace Python Projects and all things techie.
New Home for Firedrop2 SVN
Firedrop2 has a new subversion repository and Trac site.
Many thanks to the folks at Python-hosting.com for this great service.
The repository has the 0.1.3 release in a tag.
On the trunk is what is about to become Version 0.2.0. Thanks to a lot of hard work by Stewart Midwinter, this next release will include :
- Support for markdown markup
- A fix for Mac OS X support
- A switch to using ConfigObj for the config files
- Creating a blog will automatically open it in the editor
- Sensible error message if you try to create a new entry without a blog opened
- You can specify a default site to be opened when you start Firedrop
- An improved Styles menu
And possibly other things I've forgotten. When we do the release I'll make sure we have a full list of all the changes.
The version on the trunk is only suitable for new blogs. It doesn't yet have a way to migrate existing blogs to the new build.ini format. That will be added shortly.
One of the companies I have approached in looking for potential sponsors for Voidspace is Wingware. As a small company they didn't feel able to take me up on it, but they have given me a free license for their IDE.
This is very generous of them, thanks Stefan.
Up until now I've been using SPE which I've been very happy with. I'm used to the feel of SPE, so Wing feels a bit 'unfamiliar'. It's certainly more polished (which is only to be expected), but whether it offers me any tangible benefits I'm not yet decided on.
Scouting round the web it seems that the Wing debugger is very good. I'm afraid my usual method of debugging usually consists of liberal use of print statements. I'm not alone :
On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.
So I may not gain a lot of advantage from this part of Wing. One thing I do like is that it has back and forward arrows (like my favourite text editor, Ultraedit). This allows you to jump backwards and forwards between the positions you have been editing in your code. This is great if you need to go and copy a piece of code from elsewhere in your file, and then jump back to where you need to insert it into.
Struggling with Python
It's been around a while, but it's still good.
imp.find_module with py2exe
This is because the imp module doesn't understand the PEP302 importhook (which zipimport uses). That means that any code which uses imp.find_module will probably fail for programs packaged using py2exe.
I've hacked a workaround which 'monkey patches' imp to get round this. It's a hack because it uses zipmport to retrieve the bytecode object (if the module requested lives in library.zip) and then turns this back into a bytecode file. It has to create a real temporary file to do this, because imp.load_module won't accept a file like object as an argument. This means there is a cleanup function which deletes any temporary files created. You should call this at the end of your program.
If anyone has a better way, I'm all ears .
To use this code :
Save it as a module called zip_imp.py. Then in your program do :
# .. code which possibly calls imp.find_module
The initial import patches the imp module. If your zip file isn't the first path on sys.path that ends with .zip, then you will need to set zip_imp._zip_file to the path to your zip file.
from zipimport import zipimporter, ZipImportError
A module that patches ``imp.find_module`` to work from within py2exe.
You must call ``import zip_imp`` *before* importing imp.
On finishing you must call ``zip_imp.cleanup()``. This deletes any temporary
This will only work if you have a zip file on ``sys.path`.
__all__ = ['cleanup']
_zip_file = None
for entry in sys.path:
_zip_file = entry
_file_array = 
imp._find_module = imp.find_module
def _find_module(name, path=None):
return imp._find_module(name, path)
if _zip_file is None:
z = zipimporter(_zip_file)
code = z.get_code(name)
raise ImportError('Failed to find module: %s' % name)
mod = _make_pyc(code)
mod_names = [_zip_file] + name.split('.')
mod_names[-1] += '.pyc'
pathname = os.path.join(*mod_names)
description = ('.pyc', 'rb', imp.PY_COMPILED)
return (mod, pathname, description)
Turn a bytecode object back into an open file representing a '.pyc' file.
Uses the spec laid out at :
It uses the magic number from the ``imp`` module, and four null bytes to
represent the modification time of the bytecode file.
n = tempfile.mktemp()
t = open(n, 'w+b')
t.write(imp.get_magic() + chr(0)*4 + marshal.dumps(code))
imp.find_module = _find_module
"""Clean up any temporary files created when using ``load_module``."""
for name, file_obj in _file_array:
# calling close on an already closed file doesn't hurt
if __name__ == '__main__':
if not hasattr(sys, 'frozen'):
print 'This test only works from *inside* Movable Python or a',
print 'program frozen with py2exe.'
name = 'Cookie'
file, pathname, description = imp.find_module(name)
print 'Failed to ``find_module``.'
print file, pathname, description
print '``find_module`` succeeded.'
m = imp.load_module(name, file, pathname, description)
print "``load_module`` succeeded."
This code (or something like it) will be included in the next release of Movable Python .
|||A better way might be to patch imp.load_module to accept a cStringIO.StringIO instance and manually create a module object in this case. This might not work for any code that expects find_module to return a real file object and then uses the file for anything other than passing it to imp.load_module.|
|||But this code is public domain if it's useful to anyone other than me.|
Nanagram 1.4 Released
Nanagram 1.4 is now available.
On the Voidspace Shop you will find windows installers and executable versions of both the full program and the free demo. The free demo has a smaller dictionary and a few less features, but is still worth playing around with. The full version costs £4.99.
What is Nanagram ?
Nanagram is an anagram maker. Astound and annoy your friends by discovering what silly anagrams can be made from their names. It can also be used to help solve crossword puzzles. Nanagram has a friendly interface and a host of features.
Did you know that :
- George W Bush is an anagram of 'whose bugger'...
- Tony Blair : 'bony trail'...
- Ronald Reagan : 'adrenal groan' or 'loan arranged'...
- Osama Bin Laden : 'bald nose mania' or 'bad neon salami'...
- Mother Theresa : 'hot rare themes' or 'earth theorems'...
- Michael Foord : 'cool firm head'... remember, the anagram never lies
What's New in 1.4 ?
- Improved the GUI.
- Sped up the anagram finding by around 20%.
- Now built with Python 2.4 and psyco 1.5 for even more speed.
- Added the progress bar.
- Added the option to include common short words.
- Optimised sorting the results.
- We can now find anagrams where a word fits more than once into the original phrase.
Plus various other minor changes and improvements.
Voidspace Shareware Mailing List
To go along with the programs I'm releasing as Shareware, there is (another) new mailing list. This is for support, discussion, bug reports and feature requests.
IPython, matplotlib & Movable Python
This gives you the interactive features of matplotlib with Movable Python.
To get this to work you need ipython.py from the IPython distribution. You then run :
movpy ipython.py -pylab -ipythondir lib/
This assumes you run the command at the command line, from the same directory as movpy.exe. It does still writes .matplotlib\ttffont.cache to the %home% directory. There is probably a way of telling matplotlib not to do this though.
Thanks to Jens Spille for checking this for me.
Python 2.4.2 (#67, Oct 30 2005, 16:11:18) [MSC v.1310 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.7.1.fix1 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. Welcome to pylab, a matplotlib-based Python environment. For more information, type 'help(pylab)'. In : t = arange(1,50) In : y=sin(t) In : plot(t,y) Out: [<matplotlib.lines.Line2D instance at 0x01884A58>]
Movable Python as graphical caluclator.
The next release of Movable Python will let you run movpy - -pylab.
I've just launched the Voidspace Shareware Page.
This is where the programs I release as shareware will be available from. Currently this is :
Another new program release will follow in the next day or so, with more to come...
For most of these projects free demo versions will be available, and for some of them the Python source code will be available under a Shared Source Type license.
Movable Python & WebWare
I'm afraid my curiosity got the better of me, so I downloaded to see if I could find the problem. Yes, I could see the problem, it failed to start .
The error message was a slightly obscure one about a file descriptor being needed to close a file. Tracking the bug through the code (and shutting down some error handling that masked the real nature of the problem), revealed the following :
(file, pathname, description) = imp.find_module('Cookie', path)
CookieEngine = imp.load_module('Cookie', file, pathname, description)
This is from line 25 in WebKit/Cookie.py. The problem is that imp.find_module wants to return an open file object. For Movable Python (and indeed any application created by py2exe) that fails because the real Cookie module is contained within library.zip.
They have also wrapped two calls in the try: ... except: block. When the find_module call fails, the name file (shadowing a builtin guys) isn't bound. Calling file.close() then raises an obscure error because the file type expects a descriptor for that call.
What they're doing here is attempting to dynamically load the standard library Cookie module if it's available. This allows them to support Python versions < 2.2. (Is anyone really doing that ?)
A temporary solution is to change line 10 of Cookie.py to :
This gets round the problem and WebWare starts fine. For the next version of Movable Python I'll attempt to monkey patch imp.load_module so that it does something sensible (possibly extracting the file and returning a StringIO instance).
|||WebWare installation was nice and easy though.|
matplotlib and Movable Python
This means that it must be possible to get matplotlib working with py2exe.
There is a download linked to at the bottom of this post which contains the files to use matplotlib with Python 2.4.2. They should work with Movable Python or a normal Python installation.
This means that numarray also works.
Unfortunately it looks like I'm going to have to do a bit of work (although probably not much) to get IPython working in pylab mode. It should currently be possible to follow the matplotlib interactive instructions and use it from a PyCrust shell.
The matplotlib folks didn't make my life easy when it came to getting this working. I was installing it for Python 2.4 on Windows. The installation instructions say :
For standard python installations, you will also need to install either Numeric or numarray in addition to the matplotlib installer. matplotlib provides installers for Numeric and numarray users. It is important that you pick the matplotlib installer that corresponds to your array package. Ie, if you mostly work with numarray arrays, use the matplotlib numarray installer. matplotlib has a numerix setting in the matplotlib rc file (which by default resides in c:\python23\share\matplotlitb\matplotlibrc) and you should make sure this setting corresponds to your preferred array package.
So I have to choose numeric or numarray and pick the right installer for matplotlib ? Sounds easy enough.
So you go to download matplotlib, and discover that only one installer is available for Windows. It doesn't mention numeric or numarray at all. Oh well, my user has specified numarray, so I fetch that as well and install.
I run one of the example programs and it complains that it can't find numeric ! Maybe the only version they offer now is for numeric, so I look for numeric and discover that it's now called numpy and that installing that results in the same error.
Anyway - to cut a sadly longer story short, I eventually work out that the windows installer for matplotlib hasn't created a matplotlibrc file. I copy the one from here to C:\Documents and Settings\$USERNAME$\.maplotlib. Some progress - now it's complaining about an ImportError when looking for a module called gobject, which is part of GTK the default backend for matplotlib.
I edited matplotlibrc :
- I changed the default backend to TkAgg
- I set numerix to numarray
The examples now work, albeit with a host of warnings.
If you want to use matplotlib with Movable Python (or just want to save yourself some of these traumas with Python 2.4 on Windows) then you can download the following file :
It contains three directories and a single Python file. These should all be placed in your movpy/lib/ directory (or site-packages for normal Python). The matplotlib directory contains an examples sub-directory.
For the next version of Movable Python I will see if I can get ipython -pylab working. I don't foresee that being too difficult. I may need to work out how to specify a simpler location for the matplotlibrc file though.
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.