[TIP] How do I get nose to add the cwd to sys.path?

Kumar McMillan kumar.mcmillan at gmail.com
Fri Nov 14 15:42:35 PST 2008


On Thu, Nov 13, 2008 at 4:27 PM, Daryl Spitzer <daryl.spitzer at gmail.com> wrote:
> Here's what the directory structure looks like:
>
> foo/__init__.py [empty]
> foo/bar.py
> foo/tests/__init__.py [empty]
> foo/tests/bar_tests.py [contains "import bar"]
>
> I'm running `nosetests` from the foo directory.
> ...

huh, right you are.  it does not let you import bar even though your
working directory should allow you to.

Here's what happened.  Nose saw that you were running tests from a
directory containing __init__.py so it assumed you want to treat that
directory as a module.  Therefore, if you change your code to

from foo import bar

that will work without any modification to sys.path.

I'm not sure exactly why nose does this.  Jason Pellerin wrote that
code so he probably knows.  However, using absolute paths is better
practice since relative imports are deprecated in 2.6 (you get a
warning).  For extra entertainment, you can re-run your tests with
--debug=nose.importer and take note of the insert ... into sys.path
log statements.  See below for a spoiler!

>
>> btw, you could also try the nose users list.
>
> Where do I find that?

sorry, a URL would help :)
http://groups.google.com/group/nose-users?pli=1

I'd suggest bringing this up.  I'm curious what Jason's explanation is.


spoiler:

$ nosetests --debug=nose.importer
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl/foo
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: insert /Users/kumar/tmp/nose_daryl into sys.path
nose.importer: DEBUG: Import foo from /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: find module part foo (foo) in
['/Users/kumar/tmp/nose_daryl']
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl/foo
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: Import foo.tests from /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: find module part foo (foo) in
['/Users/kumar/tmp/nose_daryl']
nose.importer: DEBUG: sys.modules has foo as <module 'foo' from
'/Users/kumar/tmp/nose_daryl/foo/__init__.pyc'>
nose.importer: DEBUG: module already loaded? mod:
/Users/kumar/tmp/nose_daryl new: /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: find module part tests (foo.tests) in
['/Users/kumar/tmp/nose_daryl/foo']
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl/foo/tests
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl/foo
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: Import foo.tests.bar_tests from
/Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: Add path /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: find module part foo (foo) in
['/Users/kumar/tmp/nose_daryl']
nose.importer: DEBUG: sys.modules has foo as <module 'foo' from
'/Users/kumar/tmp/nose_daryl/foo/__init__.pyc'>
nose.importer: DEBUG: module already loaded? mod:
/Users/kumar/tmp/nose_daryl new: /Users/kumar/tmp/nose_daryl
nose.importer: DEBUG: find module part tests (foo.tests) in
['/Users/kumar/tmp/nose_daryl/foo']
nose.importer: DEBUG: sys.modules has foo.tests as <module 'foo.tests'
from '/Users/kumar/tmp/nose_daryl/foo/tests/__init__.pyc'>
nose.importer: DEBUG: module already loaded? mod:
/Users/kumar/tmp/nose_daryl/foo new: /Users/kumar/tmp/nose_daryl/foo
nose.importer: DEBUG: find module part bar_tests (foo.tests.bar_tests)
in ['/Users/kumar/tmp/nose_daryl/foo/tests']
E
======================================================================
ERROR: Failure: ImportError (No module named bar)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/nose-0.10.2-py2.4.egg/nose/loader.py",
line 363, in loadTestsFromName
    module = self.importer.importFromPath(
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/nose-0.10.2-py2.4.egg/nose/importer.py",
line 39, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/nose-0.10.2-py2.4.egg/nose/importer.py",
line 84, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/kumar/tmp/nose_daryl/foo/tests/bar_tests.py", line 2, in ?
    import bar
ImportError: No module named bar

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)



More information about the testing-in-python mailing list