Weird import failure with "nosetests --processes=1"

Hans Mulder hansmu at
Thu Nov 29 17:32:37 CET 2012

On 29/11/12 04:13:57, Roy Smith wrote:
> I've got a minimal test script:
> -----------------------------
> $ cat
> import pyza.models
> print pyza.models
> def test_foo():
>     pass
> -----------------------------
> pyza.models is a package.  Under normal conditions, I can import it fine:
> $ python
> Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import pyza.models
>>>> print pyza.models
> <module 'pyza.models' from 
> '/home/roy/deploy/current/pyza/models/__init__.pyc'>
> But when I run nosetests in parallel mode, the import fails in a way 
> which has me baffled.  What's going on here?
> $ nosetests --processes=1 -s test_foo:test_foo
> <module 'pyza.models' from 
> '/home/roy/deploy/current/pyza/models/__init__.pyc'>
> E
> ======================================================================
> ERROR: Failure: AttributeError ('module' object has no attribute 
> 'models')
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File 
> "/home/roy/production/python/local/lib/python2.7/site-packages/nose/loade
>", line 390, in loadTestsFromName
>     addr.filename, addr.module)
>   File 
> "/home/roy/production/python/local/lib/python2.7/site-packages/nose/impor
>", line 39, in importFromPath
>     return self.importFromDir(dir_path, fqname)
>   File 
> "/home/roy/production/python/local/lib/python2.7/site-packages/nose/impor
>", line 86, in importFromDir
>     mod = load_module(part_fqname, fh, filename, desc)
>   File "/home/roy/songza/pyza/djapi/", line 2, in <module>
>     print pyza.models
> AttributeError: 'module' object has no attribute 'models'
> ----------------------------------------------------------------------
> Ran 1 test in 0.107s
> FAILED (errors=1)

That is baffling indeed.  It looks like nose is adding some
directory to sys.path, which contains a module instead
of a package.

One thing you could try, is changing line 2 of you script to just

print pyza

, to see if pyza is being loaded from
/home/roy/deploy/current/pyza/__init__.pyc, as you'd expect.  If it
isn't, you'll be
one step closer to a solution.

Another idea might be to delete all *.pyc files below
/home/roy/deploy/current/ and /home/roy/songza/pyza/djapi/.
That would help if a .pyc file is somehow out of sync with
the corresponding .py file.  That's not supposed to happen,
but if it does, you can get weird results.

One last idea: put these lines at the top of

import pdb

Running under nosetest should then drop you in the debugger.
Single step into the next statement ("import pyza.models") to
see where this gets you.  It should import pyza, and then
pyza.models.  Add some print statements to the
files of those packages, so that there's somewhere for pdb
to stop and prompt.

Hope this helps,

-- HansM

More information about the Python-list mailing list