Jonathan Lange <jm...@mu...> writes:
> Hello,
>
> A module can attempt to delete itself from sys.modules (see selfabuser.py).
>
> This works perfectly well running in the normal python interpreter on
> many platforms. As evidence, Twisted does this in its reactor module.
Hm.
> One can build a console exe using py2exe 0.5, where this works perfectly.
>
> However, when you build the same script to a windows exe, it fails.
> Moreover, it fails hard. A try/except block around the offending
> statement catches nothing.
>
> I'd greatly appreciate a work-around. However, I'd appreciate an
> explanation of why this is failing even more.
>
> cheers,
> jml
> from distutils.core import setup
> import py2exe
>
> setup(windows=['crash-py2exe.py'])
>
This is crash-py2exe.py, I assume?
> f = file('debug.txt', 'w')
> f.write('Is this the real life?\n')
> f.close()
>
> import selfabuser
And this is selfabuser.py?
> f = file('debug.txt', 'a')
> f.write('Is this just fantasy?\n')
> f.close()
>
> import sys
> del sys.modules['selfabuser']
When I run this in normal Python, I get:
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import selfabuser
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: Loaded module selfabuser not found in sys.modules
>>>
So, it seems a module cannot remove itself from sys.modules - at least
not before the import is completed.
For py2exe, in 0.5.0 there's a bug in windows executables: to make them
work you should add a 'import sys' at the top of your main script.
Or use the CVS version until 0.5.1 is released, the bug is fixed there.
Thomas
|