problems with python nosetests
Question: I have problems with python nosetests. When I try to run the command, I get an
import error. I checked that the module is correctly installed on my machine.
In fact, if I run the interpreter from the directory where I run nosetests, I
am able to import the module. I checked that the problems are to import not
only that module, but different ones. Where could the fix be?

Here is a possible traceback after I run nosetests:

    
    
    Traceback (most recent call last):
      File "/Library/Python/2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
        addr.filename, addr.module)
      File "/Library/Python/2.7/site-packages/nose/importer.py", line 47, in importFromPath
        return self.importFromDir(dir_path, fqname)
      File "/Library/Python/2.7/site-packages/nose/importer.py", line 94, in importFromDir
        mod = load_module(part_fqname, fh, filename, desc)
      File "/Users/user-me/Desktop/bla/tests/blatest1", line 1, in <module>
        import a_module as mo
    ImportError: No module named a_module
    

but if I open the python interpreter, I am able to import a_module.

Here is my directory structure:

    
    
    ROOT
    └── package
        ├── __init__.py
        ├── package1
        └── tests
            ├── tests1
            │   └── package1 -> ../../package1
            └── tests2
    


Answer: Your problem has nothing to do with nose itself. nose doesn't perform any
magic additional to the Python interpreter when looking up modules and
packages.

So if we assume a directory structure like this

    
    
     ROOT
     `-- package
         |-- __init__.py
         `-- tests
             `-- __init__.py
    

and try to do `python -c import package` \- when does that work, and when
fail?

It's simple. It fails anywhere other than if you invoke the command from
within `ROOT`. Nowhere else `package` is known.

The reason is that Python keeps a list of paths (sys.path) which mark roots
for packages and modules. A statement `import package` will be taken, and
iterating through all entries in `sys.path` will be searched for either a
`package.py`, a `package`-dir with an `__init__.py` in it, or some other cases
(C-extensions, new style namespace packages)

So where does `ROOT` get into that list of paths? Simple: the Python
interpreter will always add the current working directory to the list of
paths.

To sum it up: just call nose from within `ROOT`.

There are additional ways to add a path to the `sys.path`, e.g. by using
virtualenvs. Then you can import `package` from everywhere, given you use the
venv interpreter.

