Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name MPTTModel

I did the steps at the tutorial " http://django-mptt.github.com/django-mptt/tutorial.html "

but it still give me the import error , I double check the code for the mptt and found the class MPTTMODEL exist in the mptt>model file

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
like image 683
Kareem Hashem Avatar asked Feb 19 '23 01:02

Kareem Hashem


1 Answers

One thing you could try: Go to the console and type: python (to get the python console) and then enter: from mptt.models import MPTTModel

If this gives you an error than mptt is not correctly installed.

If you are using pip you could easily do: pip install django-mptt or you might try pip install django-mptt --upgrade to update your installation. It might be that your installation did not succeeded.

If you are not using pip or virtualenv I highly recommend it. The start is a bit tough, but it is worse to install it. See these resources to get started: 1, 2

If you have successfully installed mptt and you still cannot import it, the folder where you placed mptt into is probably not accessible to python. Therefore add the path where mptt lives to your PYTHONPATH. See this thread how to do it.

like image 137
Thomas Kremmel Avatar answered Mar 03 '23 02:03

Thomas Kremmel