ImportError:无法导入名称MPTTModel

时间:2012-09-25 15:16:21

标签: django django-admin django-mptt

我完成了教程“http://django-mptt.github.com/django-mptt/tutorial.html

中的步骤

但它仍然给我导入错误,我仔细检查了mptt的代码,发现mptt>模型文件中存在类MPTTMODEL

Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1ec4710>>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/core/management/validation.py", line 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 158, in get_app_errors
    self._populate()
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 67, in _populate
    self.load_app(app_name)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/kareemhashem/espace/sharek/core/models.py", line 12, in <module>
    from mptt.models import MPTTModel, TreeForeignKey
ImportError: cannot import name MPTTModel

1 个答案:

答案 0 :(得分:6)

您可以尝试一件事:转到控制台并输入:python(以获取python控制台)然后输入:from mptt.models import MPTTModel

如果这样会给您一个错误,而不是正确安装了mptt。

如果您使用的是pip,则可以轻松执行:pip install django-mptt,或者您可以尝试pip install django-mptt --upgrade更新您的安装。可能是您的安装没有成功。

如果您不使用pip或virtualenv,我强烈推荐它。开始有点困难,但安装起来更糟糕。请参阅以下资源以开始使用:12

如果您已成功安装mptt并且仍然无法导入它,则可能无法访问您放置mptt的文件夹。因此,将mptt生活的路径添加到PYTHONPATH。请参阅此thread如何操作。