[reportlab-users] "Bad file descriptor" after py2exe or Installer

Luc Saffre reportlab-users@reportlab.com
Thu, 11 Dec 2003 20:07:22 +0200


Hello,

I had a strange problem when freezing (using either py2exe or McMillan 
installer) a script that imports reportlab (which imports PIL (which 
imports FixTk))).

- Python 2.3.3c (also with Python 2.3)
- PIL 1.1.4
- Installer or py2exe : latest versions.

Here is how I can reproduce it on a Win2000 and on a Win XP machine. I 
also found a hack that works for me, but have no idea why all this happens.

The following script `test.py` runs without problem when interpreted:

   print "before"
   from PIL import Image
   print "after"

Output as expected:

   c:\test>python test.py
   before
   after
   c:\test>

But if I freeze it and invoke the `test.exe`, I get a strange
behaviour::

   c:\test\dist> test
   before
   c:\test\dist>

The second print just does not happen. And if I redirect stderr to a 
file, I see a bit more::

   c:\test\dist> test 2> tmp.txt
   before
   c:\test\dist> type tmp.txt
   Traceback (most recent call last):
     File "test.py", line 3, in ?
       print "after"
   IOError: [Errno 9] Bad file descriptor
   c:\test\dist>

Workaround:
If I remove the following lines (41-46) of PIL/Image.py::

	try:
		 # give Tk a chance to set up the environment, in case we're
		 # using an _imaging module linked against libtcl/libtk
		 import FixTk
	except ImportError:
		 pass

then the problem disappears, and both script and executable work for
me...

If somebody sees an explanation, please let me know!

Luc Saffre