nose-devs/nose — Issue #490: nose fails to run in a cx_freeze package
Author: TheAccountWhoWasThursday
Created: 2012-01-19 00:00:58+00:00
State: open

nose.main(), when called from a cx_freeze executable on windows, fails.

This could be a problem with cx_freeze, or nose, I'm not sure. Make of it what you will.

Cx_freeze is well known, but for the sake of completeness I will provide a brief description.

Cx_freeze is the successor to py2exe. It takes a python script and packages it into a windows program directory, so the script can be run from a .exe file. Packages that the script imports can be included in a zip archive called library.zip.

My script included the nose package, used for testing my built application, and when running the exe it would fail with the message:

Traceback (most recent call last):
File "C:\Python31\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 27, in
exec(code, m.__dict__)
File "RunAllTests.py", line 42, in
File "C:\Python31\lib\site-packages\nose\core.py", line 290, in run
return TestProgram(*arg, **kw).success
File "C:\Python31\lib\site-packages\nose\core.py", line 118, in __init__
**extra_args)
File "C:\Python31\lib\unittest.py", line 1565, in __init__
self.parseArgs(argv)
File "C:\Python31\lib\site-packages\nose\core.py", line 135, in parseArgs
self.config.configure(argv, doc=self.usage())
File "C:\Python31\lib\site-packages\nose\core.py", line 253, in usage
os.path.dirname(__file__), 'usage.txt'), 'r').read()
IOError: [Errno 2] No such file or directory: 'C:\\path\\to\\my\\program\\build\\myprogram\\library.zip\\nose\\usage.txt'

This message would appear even if the 'usage.txt' file was present. Examining the file nose/core.py, line 253 reveals

> # nose was imported from a zipfile
> return ld.get_data(
> os.path.join(ld.prefix, 'nose', 'usage.txt'))
> return open(os.path.join(
> os.path.dirname(__file__), 'usage.txt'), 'r').read()

Apparently, since the nose package is in the cx_freeze library.zip, nose tries to open usage.txt as if it were in a zip file, and fails for some reason.

I fixed this issue by simply returning the empty string, ""

> # nose was imported from a zipfile
> return ""
> return ""

A hack, but it worked. I can now run my tests from a cx_freeze executable. This doesn't change anything important because usage.txt only contains how-to and usage information.

There's something wrong with the zip-archive handling, probably that the code assumes that the zip archive is a well-formed python package, which the cx_freeze library.zip is not.