4

我在教程“ http://django-mptt.github.com/django-mptt/tutorial.html ”中做了步骤

但它仍然给我导入错误,我仔细检查了 mptt 的代码,发现 mptt>model 文件中存在 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
4

1 回答 1

8

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

如果这给您一个错误,则说明 mptt 没有正确安装。

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

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

如果你已经成功安装了mptt,但仍然无法导入,那么你放置mptt的文件夹可能是python无法访问的。因此,将 mptt 所在的路径添加到您的 PYTHONPATH 中。请参阅此线程如何做到这一点。

于 2012-09-25T16:51:27.617 回答