[TIP] Nose Importer

Mark Roddy markroddy at gmail.com
Wed Apr 7 14:46:25 PDT 2010

On Wed, Apr 7, 2010 at 5:11 PM, Alfredo Deza <alfredodeza at gmail.com> wrote:
> Hi,
> I am having some issues with the Nose importer.
> My project layout is set as follows:
> /project
> /project/lib
> /project/tests
> When I run 'nosetests -v' in /project everything runs OK (tests import from
> within 'lib').
> But when I am within /project/tests 'nosetests -v' fails with an ImportError
> exception saying it cannot import the
> files from lib on only *one* of the test files.
> However, if I take the same test file that is throwing exceptions and run it
> against nose, it completes the tests successfully:
> 'nosetests -v test_permissions.py'
> It also works fine when calling it via python:
> 'python test_permissions.py'
> The one file that test_permissions.py is trying to import but is failing is:
> 'permissions', and it does so by:
> sys.path.append('../')
> from lib import hg, permissions, upgrade
> I am not sure why it would be able to import 'hg' and 'database', but fail
> to import 'permissions', and only when it runs all tests within the tests
> directory.
> Here is a copy of one of the exceptions:
> ======================================================================
> ERROR: Failure: ImportError (cannot import name permissions)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "/usr/local/lib/python2.6/dist-packages/nose/loader.py", line 382, in
> loadTestsFromName
>     addr.filename, addr.module)
>   File "/usr/local/lib/python2.6/dist-packages/nose/importer.py", line 39,
> in importFromPath
>     return self.importFromDir(dir_path, fqname)
>   File "/usr/local/lib/python2.6/dist-packages/nose/importer.py", line 86,
> in importFromDir
>     mod = load_module(part_fqname, fh, filename, desc)
>   File "/home/alfredo/pacha/tests/test_permissions.py", line 10, in <module>
>     from lib import hg, permissions, database
> ImportError: cannot import name permissions
> Maybe I need to import differently? But then again... *why* it works when I
> call it directly? I basically want
> to be able to run from either /project or /project/tests with the same
> results.
> --
> Alfredo Deza
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

One possibility for why you can import 'hg' from lib but not
'permissions' is that 'hg' module that gets imported is not actually
the one you expect (when running from the 'tests' directory).  I ran
into this the other day when I had my path messed up, and I could
import one module from a package but not another.  Turns out the one I
could import was under site-packages as I had an older version
installed which did not have the module that the import failed on.
You can check that the 'hg' module which gets imported is actually the
module you expect by looking at it's __file__ attribute.


More information about the testing-in-python mailing list