nosetests vs. relative imports?


Roy Smith

Ubuntu Linux 12.04 (precise)
Python 2.7.3

I have a package (i.e. a directory with a file). In that
directory, I have files and; contains
the single line:

from .math import EMA

If I run on the command line, "nosetests" everything is
fine (it reports "Ran 0 tests in 0.001s", which is what I expect, since
I haven't written any tests yet).

But, if I leave off the .py, I get:

$ nosetests test_math
ERROR: Failure: ValueError (Attempted relative import in non-package)
Traceback (most recent call last):
"/home/roy/deploy/current/python/local/lib/python2.7/site-packages/nose/l", line 413, in loadTestsFromName
addr.filename, addr.module)
"/home/roy/deploy/current/python/local/lib/python2.7/site-packages/nose/i", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
"/home/roy/deploy/current/python/local/lib/python2.7/site-packages/nose/i", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/roy/deploy/current/code/songza/util/", line 1,
in <module>
from .math import EMA
ValueError: Attempted relative import in non-package

Ran 1 test in 0.001s

What is going on that nose can't deal with the relative import when run
that way?

Nose has always felt like a magic ring. I'm really glad I discovered it
because it makes my life so much better. But I'm never quite sure I
really understand everything it's doing.



Tony the Tiger

There already is a module math in Python. You probably have it. Name your
own module something else besides 'math' and try again.

Could that be it?


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question