Opened 11 years ago

Closed 6 years ago

#2489 defect closed duplicate (duplicate)

trial.py script fails on Windows with Python 2.5

Reported by: arkanes Owned by: Jonathan Lange
Priority: normal Milestone:
Component: trial Keywords:
Cc: teratorn, ivank Branch:
Author:

Description (last modified by Jean-Paul Calderone)

Python 2.5 made a change in the default items on sys.path - instead of the current working directory, it's the directory of the running script.

This means that the C:\Python25\Scripts\trial.py helper script doesn't work as expected unless your local directory is in PYTHONPATH for other reasons (like when running the Twisted test suite, for example).

The actual failure was very hard to diagnose, because it happens quite high up the callstack and results in an a cascade failure that doesn't manifest until much farther down, somewhere in t.p.modules. When t.trial.runner fails to find the module, it uses a fallback which ends up identifying the __init__.py file of the module, rather than the module itself. I've attached a copy of the actual traceback that happens with a stock trial.py, and another with an enhanced traceback from cgitb. If you look at the enhanced traceback, you'll see that the module is shown as <module '__init__' from 'testcopy\__init__.pyc'>, rather than <module 'testcopy' from 'testcopy\__init__.pyc'>.

This is the cascading error from t.trial.runner.filenameToModule, which falls back on _importFromFile(), which imports the module but does so with the file name rather than the package name.

There's an easy and dirty fix - modifying the trial.py runnner to add os.curdir to the path. Fixing filenameToModule to create the module correctly might also work doesn't seem to be enough - the correct module name is returned, but it's still not insterted into the import cache.

Attachments (2)

importerrors.txt (3.8 KB) - added by arkanes 11 years ago.
Standard traceback
importerrors_cgitb.txt (12.4 KB) - added by arkanes 11 years ago.
cgitb traceback, last time I promise

Download all attachments as: .zip

Change History (10)

Changed 11 years ago by arkanes

Attachment: importerrors.txt added

Standard traceback

comment:1 Changed 11 years ago by Jean-Paul Calderone

Description: modified (diff)

reformatting non-markup

comment:2 Changed 11 years ago by Jean-Paul Calderone

importerrors_cgitb.txt looks pretty short. I think you attached the wrong file?

Changed 11 years ago by arkanes

Attachment: importerrors_cgitb.txt added

cgitb traceback, last time I promise

comment:3 in reply to:  2 Changed 11 years ago by arkanes

Replying to exarkun:

importerrors_cgitb.txt looks pretty short. I think you attached the wrong file?

Yes, sorry. And then I attached one run against the wrong version of runners.py. This one should be accurate.

In looking for a "clean" fix I'm not sure that the fallback code in runners.py ever worked. It creates a module by filename, but imp.load_source doesn't add the module to sys.modules or to sys.path_importer_cache, so any use of the fallback code (because of the Pyhon 2.5 change or not) will end up raising this same error.

comment:4 Changed 11 years ago by zseil

Note that the trial script already uses the "easy and dirty fix" on all platforms that have os.getuid if it isn't ran by root.

comment:5 Changed 11 years ago by teratorn

Cc: teratorn added

comment:6 Changed 11 years ago by Jean-Paul Calderone

#2005 and #2507 are probably related to this.

comment:7 Changed 8 years ago by ivank

Cc: ivank added

I'll just note that when a program is started like this in current directory C:\temp,

C:\Python27\python.exe C:\Python27\Scripts\somescript.py

, both C:\temp and C:\Python27\Scripts are in sys.path

When started like this:

C:\Python27\Scripts\somescript.py

, C:\temp is not in sys.path (but C:\Python27\Scripts is).

In both cases, os.getcwd() returns C:\temp.

If you run trial with the explicit python.exe, and remove the getuid security feature (?) from the preamble (always insert the current directory), trial seems to work pretty well on Windows.

comment:8 Changed 6 years ago by zseil

Resolution: duplicate
Status: newclosed

This was fixed as part of #3526. cwd is now prepended to sys.path in trial and twistd scripts, both when installed and when running from a checkout.

Note: See TracTickets for help on using tickets.