ImportError: cannot import name MPTTModel - django

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

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.

Related

Why I get error raise ImproperlyConfigured

I want to add info in database using django-seed.
seed.py
from django_seed import Seed
from models import Employee
seeder = Seed.seeder()
seeder.add_entity(Employee, 5)
inserted_pks = seeder.execute()
When I try to start command: python3 seed.py
I've got this error:
Traceback (most recent call last):
File "seed.py", line 2, in <module>
from models import Employee
File "/home/kaucap/test/myproject/app_worker/models.py", line 4, in <module>
class Employee(models.Model):
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/db/models/base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/apps/registry.py", line 137, in check_apps_ready
settings.INSTALLED_APPS
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 87, in __getattr__
self._setup(name)
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 67, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Can anyone explain why I have this error and how I can fix it?
python3 seed.py started python without Django settings.
if you are want to start seed.py in Django "environment",
in your django-pproject folder:
python3 manage.py shell
Django shell starts, and in shell:
import seed.py
If you see the same error - you have not created any Django project yet.

ImportError: cannot import name SortedDict - only on Python 2.7, not 2.6

I am trying to get a Django application up and running. The libraries are a bit out of date (its currently on Django 1.4).
I have got it more or less working when I tried python 2.6, but when I change to a virtualenv using python 2.7 I get the error:
Unhandled exception in thread started by <function wrapper at 0x7f057b17c320>
Traceback (most recent call last):
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/colin/software/virtualenvs/barshool/lib/python2.7/site-packages/transmeta/__init__.py", line 7, in <module>
from django.utils.datastructures import SortedDict
ImportError: cannot import name SortedDict
I set up the virtualenvs with the same requirements files.
What is the difference between the versions that could be causing this?
SortedDict was removed in Django 1.9. Make sure you have installed the same version of Django in the new virtual environment as the old one.
If it was your own code that was using SortedDict, the long term fix would be to update the code to use collections.OrderedDict, which was added in Python 2.7. In your case, it looks like the import error is coming from a third party app, which you'll have to update or replace to make the code work with Django 1.9.
This seems a persistent bug. Meant the same issue while working with django==1.11.1 and djangorestframework==3.9.1. It was particularly djangorestframework (drf) which was importing the sortedDict, so i instead updated drf. The latest version at this time is djangorestframework==3.10.3 from the python foundation. This drf upgrade solved my challenge.
Note: I was working with python 3.6.8, on Ubuntu 18.04.3 LTS (just in case you need those specifics)

Django cms - running the installer

I am trying out the django cms. I have now tried the recipe described here: http://docs.django-cms.org/en/support-3.0.x/introduction/install_from_scratch.html very carefully, but towards the end of the installer run it fails with an exception:
...
raise exceptions.UnknownMigration(self, sys.exc_info())
south.exceptions.UnknownMigration: Migration 'auth:0001_initial' probably doesn't exist.
Traceback (most recent call last):
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/south/migration/base.py", line 313, in migration
migration = __import__(full_name, {}, {}, ['Migration'])
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/django/contrib/auth/migrations/0001_initial.py", line 5, in <module>
from django.db import models, migrations
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/django/db/migrations/__init__.py", line 2, in <module>
from .operations import * # NOQA
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/django/db/migrations/operations/__init__.py", line 1, in <module>
from .models import (CreateModel, DeleteModel, AlterModelTable,
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 4, in <module>
from django.db.models.options import normalize_together
ImportError: cannot import name normalize_together
Traceback (most recent call last):
File "/tmp/munin-cloud/venv/bin/djangocms", line 11, in <module>
sys.exit(execute())
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/djangocms_installer/main.py", line 31, in execute
django.setup_database(config_data)
File "/tmp/munin-cloud/venv/local/lib/python2.7/site-packages/djangocms_installer/django/__init__.py", line 325, in setup_database
"manage.py", "migrate", "--fake"], env=env)
File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError:
Command '['/tmp/munin-cloud/venv/bin/python', '-W', 'ignore', 'manage.py', 'migrate', '--fake']'
returned non-zero exit status 1
When running manage.pg syncdb it reports that some of the tables have been synced, and some are not synced - use migrations. When I then run manage.py migrate I get the same exception again.
I am on Ubuntu 14.04 - if that matters.
I mixed up the django version in the current virtual environment and the django version configured during the django-cms install process. When I ensured that I had Django 1.6 installed and entered 1.6 during django-cms configuration it worked.

Django-registration compatibility issue with django 1.7

I've been using [django-registration] (https://bitbucket.org/ubernostrum/django-registration) and now I have started using django 1.7b1 and here is the error I am getting the error copied below. It is being raised from django-registration in models.py:
try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError:
from django.contrib.auth.models import User
and it seems it is being raised because get_user_model() is being called before the app registry is ready. I am not sure if this a compatibility issue or not, if yes is there a simple workaround for this? and if not can you help me identify what I am doing wrong?
RuntimeError: App registry isn't ready yet.
File "/Users/nima/pe-dev/manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/core/management/__init__.py", line 391, in execute
django.setup()
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/apps/registry.py", line 106, in populate
app_config.import_models(all_models)
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/apps/config.py", line 190, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/registration/models.py", line 15, in <module>
User = get_user_model()
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/contrib/auth/__init__.py", line 136, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL)
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/apps/registry.py", line 187, in get_model
self.check_ready()
File "/Library/Python/2.7/site-packages/Django-1.7b1-py2.7.egg/django/apps/registry.py", line 119, in check_ready
raise RuntimeError("App registry isn't ready yet.")
Don't use the django-registration available from PyPI. It does not support Django 1.7 and it appears it never will. The repo maintainer has abdicated and the project appears unmaintained.
There is a maintenance fork available on Github which has worked well for me with Django 1.7:
https://github.com/macropin/django-registration
It's available from PyPI as django-registration-redux.
https://pypi.python.org/pypi/django-registration-redux/
You can install using pip:
pip install django-registration-redux
This note addresses your problem.
I think the preferred way to import User is:
from django.conf import settings
User = settings.AUTH_USER_MODEL
EDIT:
Looks like this problem has been noted but the project admin is being difficult about making the change. link. This is a bigger problem with the updates in Django 1.7.
I would say you could either: (1) fork the repo and make the change yourself, or (2) make the changes in your site packages folder after you pip install. The latter version won't work as well if you then push it to another server and install with requirements.txt. Note that if you do option 1 with requirements.txt, you'll want to point it to your repo rather than Django-registration.

django 1.3 server error

I am using django version 1.3. Python manage.py runserver 0.0.0.0:8000 gives the following error.
Validating models...
Unhandled exception in thread started by < bound method Command.inner_run of < django.contrib.staticfiles.management.commands.runserver.Command object at 0x20eb110>>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
self.validate(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 36, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 64, in _populate
self.load_app(app_name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 78, in load_app
models = import_module('.models', app_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/local/lib/python2.7/dist-packages/tastypie/models.py", line 6, in <module>
from tastypie.utils import now
File "/usr/local/lib/python2.7/dist-packages/tastypie/utils/__init__.py", line 1, in <module>
from tastypie.utils.dict import dict_strip_unicode_keys
File "/usr/local/lib/python2.7/dist-packages/tastypie/utils/dict.py", line 1, in <module>
from django.utils.encoding import smart_bytes
ImportError: cannot import name smart_bytes
Its working fine with django 1.4.2+, I was using django1.4 then upgraded it
pip install django==1.4.5 --upgrade
Django 1.3 had the issues of smart_bytes, and from your traceback it shows the same error, its been resolved in Django 1.4, Try upgrading Django to the latest version of 1.5.
See https://github.com/django/django/blob/master/django/utils/encoding.py#L149
Also, always check the release notes before upgrading your Django.
Update:
Sorry about that, wow, what a brainfart ;).
I don't know the full history of that function, but looking at django.utils.encoding on Github.. tagged 1.3.7; this file does not contain the function that Tastypie is attempting to import. https://github.com/django/django/blob/1.3.7/django/utils/encoding.py
If you cannot upgrade Django, you'll need to patch your Tastypie.utils.dict file to work with what you have.