4

I have the following import line in a Django 1.4 project running in Python 2.7.6:

from django.contrib.gis.utils import GeoIP

This line works just fine 100% of the time in production and in development, but this line fails with an ImportError in test--sometimes. Given the following dummy test:

from django.test import TestCase
from django.test.client import Client

class DummyTestCase(TestCase):
    def test_GET_200_response_code(self):
        c = Client()
        response = c.get('/')
        self.assertEqual(response.status_code, 200)

If I run only the tests in this module, it fails on the response = self.c.get('/') line:

...
File "...", line 19, in <module>  
    from django.contrib.gis.utils import GeoIP
ImportError: cannot import name GeoIP

However when I run the entire test suite, including this same test, it passes without problems. And, again, the import works fine in console, and in the server in dev and production.

So... why is that happening? Why would I get an ImportError only when running this single test module and never otherwise, even if I run all the tests including this module?

Note: I'm using Django 1.4 right now. I'm aware that the import path changes and will be from django.contrib.gis.geoip import GeoIP, but the shortcut from utils is still there in 1.4, until 1.6. Either way, switching the import path demonstrates the same behavior as described above.

ryanmrubin
  • 728
  • 4
  • 11

1 Answers1

-1

I think here's your answer, looks like since django 1.4 it's in different package : django.contrib.gis.geoip and django.contrib.gis.utils shortcut has been removed in django 1.6, so I would recommend to investigate django version on test server. At first try to change import to django.contrib.gis.geoip if using django>=1.4, and you won't have to investigate nothing.

mdargacz
  • 1,267
  • 18
  • 32
  • Please see my note at the bottom of my original post. The old import path still works in 1.4, and even changing it does not solve this problem. – ryanmrubin Sep 03 '15 at 14:23