简体   繁体   中英

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.

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...

I do not know the exact reason, but using Django's python shell ie {$./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.

I had the same error. Uninstalling and reinstalling django took care of it:

sudo pip uninstall django
sudo pip install django

add django to your sys path. I had a similar issue and it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM