Tuesday, September 21, 2010

Python and cherrypy on OSX

While running a cherrypy application I got this error:
ImportError: No module named header

The problem and the solution is described here http://old.nabble.com/py2exe-fails-with-%22%22-td18029166.html

>
> .... Creating the exe file works but running it fails with:
>
> Traceback (most recent call last):
>   File "bootstrap.py", line 2, in  
>   File "cherrypy\__init__.pyc", line 164, in  
>   File "cherrypy\_cperror.pyc", line 6, in  
>   File "cherrypy\lib\http.pyc", line 23, in  
>   File "email\__init__.pyc", line 79, in __getattr__
> ImportError: No module named header
>
>
> For that particular import (email.Header) you can upgrade to trunk
> HEAD or apply this small patch: http://www.cherrypy.org/changeset/1967.
> That email module is only used for decoding RFC-2047 text, which hardly any clients ever do.
>
                                                                  
The solution is to modify the file: /sw/lib/python2.6/cherrypy/lib/http.py (this is the path particular of my machine)


Index: trunk/cherrypy/lib/http.py
===================================================================
--- trunk/cherrypy/lib/http.py (revision 1836)
+++ trunk/cherrypy/lib/http.py (revision 1967)
@@ -21,5 +21,4 @@
 
 import cgi
-from email.Header import Header, decode_header
 import re
 from rfc822 import formatdate as HTTPDate
@@ -197,4 +196,5 @@
 def decode_TEXT(value):
     """Decode RFC-2047 TEXT (e.g. "=?utf-8?q?f=C3=BCr?=" -> u"f\xfcr")."""
+    from email.Header import decode_header
     atoms = decode_header(value)
     decodedvalue = ""
@@ -366,4 +366,5 @@
                         # Encode RFC-2047 TEXT
                         # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
+                        from email.Header import Header
                         v = Header(v, 'utf-8').encode()
                     else:


The patch is also available at: http://www.cherrypy.org/changeset/1967

No comments:

Post a Comment