Question

I am having an issue in my Django project where I am importing the same module twice. This is causing a unit test of mine to fail: this unit test checks that the view found by resolving a URL is the same view imported from within the app being tested. So, my tests file looks something like:

from django.core.urlresolvers import resolve
from django.tests import TestCase
from .views import index

class IndexText(TestCase):
    def test_root_url_resolves_to_index_view(self):
        found = resolve('/someapp/')
        import pdb; pdb.set_trace()           # Using this to debug because the below assertion is failing
        self.assertEqual(found.func, index)

The above assertion is failing because the two functions are not equal. When debugging in pdb, I found that found.func.__module__ is someapp.views while index.__module__ is projectName.someapp.views.

I was told in #django on Freenode this could be because although I had recently updated to Django 1.6, I was using the old 1.3 project structure, where the project settings.py and urls.py are in the root of the project alongside manage.py.

I've fixed that by creating a new directory within my project root, with the same name as the directory containing the project root, and placed my urls.py and settings.py in that directory. So, my directory structure looks something like this:

/home
  /joseph
    /myWorkspace
      /projectName
        manage.py
        /projectName
          __init__.py
          urls.py
          settings.py
        /someapp
          __init__.py
          views.py
          tests.py
        /someotherapp
        / ... and so on ...

When I open a shell via manage.py shell, import sys, and print sys.path, the first directory in that list is /home/joseph/myWorkspace/projectName and that seems right to me. The rest of the python path looks pretty normal, pointing to different site packages, etc.

However, when I run my test from above via manage.py test someapp, if I print the sys.path in pdb, I see that my python path first contains /home/joseph/myWorkspace, and also /home/joseph/myWorkspace/projectName. This does not seem correct to me, and I think this may be why I am having issues with double imports.

I am not setting PYTHONPATH in my environment variables. As far as I know, I am also not making any adjustments to sys.path within my apps or settings.

I don't know where to go from here, can anyone lend some insight?

Was it helpful?

Solution

As @Mikko Ohtamaa mentioned, the issue where was that the project root directory contained an __init__.py

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top