当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: