Path issues, dependent on global test case run vs specific test module run
See original GitHub issueThe setup for my current project is that the project root directory has a certain name, and there is a directory within the root, of the same name, which in turn contains all the app directories. So:
/path/to/project_name/ /path/to/project_name/project_name/ /path/to/project_name/project_name/app_1/ /path/to/project_name/project_name/app_2/ /path/to/project_name/project_name/app_3/
And so on. When I run:
REUSE_DB=1 ./manage.py test
It passes many tests, but fails whenever the test case calls reverse():
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 476, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 363, in _reverse_with_prefix
possibilities = self.reverse_dict.getlist(lookup_view)
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 276, in reverse_dict
self._populate()
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 242, in _populate
for pattern in reversed(self.url_patterns):
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 328, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 323, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/utils/importlib.py", line 41, in import_module
__import__(name)
ImportError: No module named urls
Doing some debugging, it looks like it’s failing when trying to import “project_name.urls”. I realized that it was getting confused, because /path/to/project_name/urls.py doesn’t exist, but /path/to/project_name/project_name/urls.py does. If I rename the first “project_name” directory (which my teammates prefer not to do), it fixes it:
/path/to/blah/project_name/app_1 /path/to/blah/project_name/urls.py
And so forth.
Interestingly, the problem ALSO goes away (without renaming anything) when I run a specific test module:
REUSE_DB=1 ./manage.py test project_name.app_1.tests
I tried looking at the offending import line. The name it’s importing, and the sys.path at that point, are identical. Even in the sense of basic Python, I don’t understand how this is possible, but maybe there’s something I don’t understand about the importing process.
Is this a bug in Django Nose? In either case, is there something else I could try with the sys.path that I’m missing?
Thanks.
Issue Analytics
- State:
- Created 11 years ago
- Comments:5 (1 by maintainers)
I had a similar problem, when running unittests with nose in django 1.11, some of my tests were failing with: No Module Named xyz.urls my workaround was to add the following in settings.py, ie amended my ROOT_URLCONF from xyz.urls to xyz.xyz.urls
hope this might be of use to others who have similar problems
I believe this goes away with the Django 1.4 project layout. I’ll re-open if someone is still bit by this in 1.4+.