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

Daryl Spitzer daryl.spitzer at gmail.com
Sun Nov 16 06:21:49 PST 2008


On Sat, Nov 15, 2008 at 3:52 PM, Daryl Spitzer <daryl.spitzer at gmail.com> wrote:
> On Fri, Nov 14, 2008 at 3:42 PM, Kumar McMillan

>> For extra entertainment, you can re-run your tests with
>> --debug=nose.importer and take note of the insert ... into sys.path
>> log statements.
>
> That's quite useful.  Did I miss seeing that in the nose documentation
> somewhere, or is that an undocumented feature?

I withdraw this question.  I looked too quickly and missed it in
nosetests usage.

--
Daryl


On Sat, Nov 15, 2008 at 3:52 PM, Daryl Spitzer <daryl.spitzer at gmail.com> wrote:
> On Fri, Nov 14, 2008 at 3:42 PM, Kumar McMillan
> <kumar.mcmillan at gmail.com> wrote:
>
>> 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.
>
> Yep, that worked in my test.  It didn't work in my "real" code, and
> then I realized that "foo" was itself in a package, and so on.  (I'm
> adding unit tests to some legacy code.)  So I had to use something
> like:
>
> from kilroy.was.here.foo import bar
>
>> However, using absolute paths is better practice since relative
>> imports are deprecated in 2.6 (you get a warning).
>
> I understand that better now after reading
> http://www.python.org/dev/peps/pep-0328/ over a couple times.
>
>> For extra entertainment, you can re-run your tests with
>> --debug=nose.importer and take note of the insert ... into sys.path
>> log statements.
>
> That's quite useful.  Did I miss seeing that in the nose documentation
> somewhere, or is that an undocumented feature?
>
>>>> 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
>
> Thanks.  I joined up.
>
>> I'd suggest bringing this up.  I'm curious what Jason's explanation is.
>
> I don't need to, since Jason replied to this thread.
>
> Thanks for your help Kumar.
>
> --
> Daryl
>
>
> On Fri, Nov 14, 2008 at 3:42 PM, Kumar McMillan
> <kumar.mcmillan at gmail.com> wrote:
>> 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