Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from django.db import utils ImportError cannot import name utils?

I am in the plain python shell and I am getting this error when trying to import my project models:

from results.models import TestResult  

Traceback (most recent call last):  
  File "C:\Program Files (x86)\Wing IDE 3.2\src\debug\tserver\_sandbox.py", line 1, in <module>  
    # Used internally for debug sandbox under external interpreter  
  File "C:\Users\audrey_moreau\myProject\results\models.py", line 1, in <module>  
    from django.db import models  
  File "c:\Python27\Lib\site-packages\django\db\__init__.py", line 40, in <module>  
    backend = load_backend(connection.settings_dict['ENGINE'])  
  File "c:\Python27\Lib\site-packages\django\db\__init__.py", line 34, in __getattr__  
    return getattr(connections[DEFAULT_DB_ALIAS], item)  
  File "c:\Python27\Lib\site-packages\django\db\utils.py", line 92, in __getitem__  
    backend = load_backend(db['ENGINE'])  
  File "c:\Python27\Lib\site-packages\django\db\utils.py", line 54, in load_backend  
    return import_module('.base', backend_name)  
  File "c:\Python27\Lib\site-packages\django\utils\importlib.py", line 35, in import_module  
    __import__(name)  
  File "c:\Python27\Lib\site-packages\django\db\backends\sqlite3\base.py", line 14, in <module>  
    from django.db import utils  
ImportError: cannot import name utils

Can anyone give me a pointer on how to fix this? I am using Python 2.7.

like image 828
user1598655 Avatar asked Aug 24 '12 15:08

user1598655


2 Answers

I had this bug and it was caused by django_nose. I was trying to import django_nose from settings.py to determine if it exists on the system like this:

try:
    import django_nose
    INSTALLED_APPS += ['django_nose']
    TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
except ImportError:
    pass

I modified this to

from imp import find_module
try:
    find_module('django_nose')
    INSTALLED_APPS += ['django_nose']
    TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
except ImportError:
    pass

and my issue was resolved...

like image 149
user920391 Avatar answered Oct 12 '22 18:10

user920391


I do not know the exact reason, but using Django's python shell i.e {$./manage.py shell} does not throw the error. I think Django does it's own little customization/overriding of python's packages, hence the altercation in the traditional interpreter.

like image 25
Shishir Biyyala Avatar answered Oct 12 '22 18:10

Shishir Biyyala