[TIP] ImportError (No module named 'pytest') - weird coverage and py interaction as a result of tox dependency, manuel triggering it...

Chris Withers chris at simplistix.co.uk
Thu Jul 3 02:40:23 PDT 2014

On 03/07/2014 09:30, Chris Withers wrote:
> Hi All,
> So, with:
> nosetests --with-xunit --with-cov
> ...I get the following failures on Python 3.* in Linux and Mac:
> ERROR: Failure: ImportError (No module named 'pytest')
> Full and nasty traceback here:
> http://jenkins.simplistix.co.uk/job/testfixtures-virtualenv/PYTHON=3.4,label=linux/333/testReport/junit/nose.failure/Failure/runTest/
> The weird thing is that if I drop the --with-cov, things work fine.

Some more digging has revealed something interesting.

So, by the time the problematic import statement in test_docs.py is his, 
when nose is run --with-cov, sys.modules has 310 keys versus the 260 it 
has when run without --with-cov.

When run without coverage, they're all the same type:

(Pdb) set(type(v) for v in sys.modules.values())
{<class 'module'>}

When run with coverage, we get a bunch of things:

(Pdb) set(type(v) for v in sys.modules.values())
<class 'py._apipkg.ApiModule'>,
<class 'py._apipkg.AliasModule.<locals>.AliasModule'>,
<class 'py._apipkg.AliasModule.<locals>.AliasModule'>,
<class 'module'>,
<class 'py._apipkg.AliasModule.<locals>.AliasModule'>,
<class 'py._apipkg.AliasModule.<locals>.AliasModule'>

...one of which causes problems when the manuel package's introspection 
tries to do:

  24  -> def absolute_import(name):
  25         module_path = normalize_module_path(imp.find_module(name)[1])
  27         # don't create a new module object if there's already one 
that we can reuse
  28         for module in list(sys.modules.values()):
  29             if module is None or not hasattr(module, '__file__'):
  30                 continue
  31             if module_path == normalize_module_path(module.__file__):
  32                 return module

(Pdb) hasattr(sys.modules['py.test'], '__file__')
*** ImportError: No module named 'pytest'

So, any ideas?


Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk

More information about the testing-in-python mailing list