I can\'t seem to get the nose testing framework to recognize modules beneath my test script in the file structure. I\'ve set up the simplest example that demonstrates the p
Are you in a virtualenv? In my case, nosetests
was the one in /usr/bin/nosetests
, which was using /usr/bin/python
. The packages in the virtualenv definitely won't be in the system path. The following fixed this:
source myvirtualenv/activate
pip install nose
which nosetests
/home/me/myvirtualenv/bin/nosetests
For example, with the following directory structure, if you want to run nosetests
in m1
, m2
or m3
to test some functions in n.py
, you should use from m2.m3 import n
in test.py
.
m1
└── m2
├── __init__.py
└── m3
├── __init__.py
├── n.py
└── test
└── test.py
I got this error message because I run the nosetests
command from the wrong directory.
Silly, but happens.
Another potential problem appears to be hyphens/dashes in the directory tree. I recently fixed a nose ImportError issue by renaming a directory from sub-dir
to sub_dir
.
Of course if you have a syntax error in the module being imported that will cause this. For me the problem reared its head when I had a backup of a tests file with a path like module/tests.bak.py in the same directory as tests.py. Also, to deal with the init package/module problem in a Django app, you can run the following (in a bash/OSX shell) to make sure you don't have any init.pyc files lying around:
find . -name '*.pyc' -delete
You've got an __init__.py
in your top level directory. That makes it a package. If you remove it, your nosetests
should work.
If you don't remove it, you'll have to change your import
to import dir.foo
, where dir
is the name of your directory.