Running a python script with nose.run(...) from outside of the script's directory results in AttributeError: 'module' object has no attribute 'tests'
Question: I have a python application with a few sub directories. Each subdirectory has
its own `tests.py` file.

I use nose to run all of the unittests across all of these files in one shot,
by creating a script `run_unit_tests.py` that calls `nose.run(...)`.

If I am inside of the directory containing `run_unit_tests.py`, everything
works fine. However, if I am anywhere else on the file system, it fails with
AttributeError: 'module' object has no attribute 'tests'.

Here is something similar to my directory structure:

    
    
    MyApp/
        foo/
            __init__.py
            tests.py
            bar/
                __init__.py
                tests.py
        run_unit_tests.py
    

In my `run_unit_tests.py`:

    
    
    class MyPlugin(Plugin):
        ...
    
    if __name__ == '__main__':
        nose.run(argv=['', 'foo.tests', '--with-my-plugin'])
        nose.run(argv=['', 'foo.bar.tests', '--with-my-plugin'])
    

If I run `run_unit_tests.py` while inside the top `MyApp` directory,
everything works fine.

However, if I run the script while in some other folder on the file system, it
fails with:

    
    
    ======================================================================
    ERROR: Failure: AttributeError ('module' object has no attribute 'tests')
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/apps/Python/lib/python2.7/site-packages/nose/loader.py", line 407, in loadTestsFromName
        module = resolve_name(addr.module)
      File "/apps/Python/lib/python2.7/site-packages/nose/util.py", line 322, in resolve_name
        obj = getattr(obj, part)
    AttributeError: 'module' object has no attribute 'tests'
    

* * *

In fact, if I add the following to `run_unit_tests.py`, it works fine:

    
    
    import os
    os.chdir('/path/to/MyApp')
    

What can I change inside of my nose script such that I can run the script from
outside of the directory?


Answer: Actually, you want to be careful here. Because, the reason why this is
happening is because your imports in your test are with respect to:
`/path/to/MyApp`.

So, when you run your tests from that working directory, your unit test files
are all importing with respect to that directory being the project source. If
you change directories and run from another location, that now becomes your
root, and your imports will surely fail.

This could bring different opinions, but I usually make sure my sources are
all referenced from the same project root. So if we are here:

    
    
    MyApp/
        foo/
            __init__.py
            tests.py
            bar/
                __init__.py
                tests.py
        run_unit_tests.py
    

I would run everything from within `MyApp`

Furthemore, I would consider creating a `tests` directory and putting all your
tests in that directory, making your imports easier to manage and better
segregating your code. However, this is just an opinion, please don't feel
like this is a necessity. Whatever works for you, go with it.

Hope this helps.

