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

Daryl Spitzer daryl.spitzer at gmail.com
Thu Nov 13 14:27:51 PST 2008

Here's what the directory structure looks like:

foo/__init__.py [empty]
foo/tests/__init__.py [empty]
foo/tests/bar_tests.py [contains "import bar"]

I'm running `nosetests` from the foo directory.

I read the following in http://somethingaboutorange.com/mrl/projects/nose/:

"When nose imports a module, it adds that module's directory to
sys.path; when the module is inside of a package, like package.module,
it will be loaded as package.module and the directory of package will
be added to sys.path."

So I expected it to just work.  I'm sure it's user error.  Perhaps I
should try actually creating the files above and see if it works for

> btw, you could also try the nose users list.

Where do I find that?  (I didn't see it on



On Thu, Nov 13, 2008 at 2:16 PM, Kumar McMillan
<kumar.mcmillan at gmail.com> wrote:
> On Thu, Nov 13, 2008 at 3:49 PM, Daryl Spitzer <daryl.spitzer at gmail.com> wrote:
>> When I run `nosetests` it finds the tests in the .py files in tests/,
>> but I get an import error in the tests when they try to import the
>> module they're testing (which is in the parent directory of the tests
>> in "tests").  I've tried `nosetests -w .` (and other variants) but I
>> get the same import errors.
>> ...
>> if not os.getcwd() in sys.path:
>>    sys.path.append(os.getcwd())
> This is pretty much what nose does for you so.  Your use case is very
> common and should work just fine with default options.  How is your
> test directory structure laid out?  Do you use __init__py files in
> your test directories?
> You *could* try nosetests --no-path-adjustment but path adjustment is
> usually there to make things work how you'd expect.  btw, you could
> also try the nose users list.
> Kumar

More information about the testing-in-python mailing list