Search code examples
pythondjangopydev

Apps aren't loaded yet while doing the official Django tutorial


I'm facing an issue with the Django settings while doing the official Django tutorial . The PyDev console returns an error Apps aren't loaded yet when I execute the debug mode (F11) on the file models.py. It's a very simple project in which I only changed the project name to 'capital' and the application's name to 'marketsdata'.

The workarounds proposed in this similar question do not solve my problem. Django 1.7 upgrade error: AppRegistryNotReady: Apps aren't loaded yet

How is it possible the official tutorial returns such error with the basics settings.py and models.py files? what I'm doing wrong?

models.py

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

settings.py

INSTALLED_APPS = [
    'marketsdata.apps.MarketsdataConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

apps.py

from django.apps import AppConfig


class MarketsdataConfig(AppConfig):
    name = 'marketsdata'

urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('marketsdata/', include('marketsdata.urls')),
    path('admin/', admin.site.urls),
]

Debuger output:

pydev debugger: starting (pid: 23504)
Traceback (most recent call last):
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 3129, in <module>
    main()
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 3122, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 2195, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 2202, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/_pydev_imps/_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/abc/python/capital/marketsdata/models.py", line 3, in <module>
    class Question(models.Model):
  File "/home/abc/.local/lib/python3.6/site-packages/django/db/models/base.py", line 107, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/abc/.local/lib/python3.6/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/home/abc/.local/lib/python3.6/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Solution

  • For PyDev to run a django project you have to right-click the project and do a:

    Run as > PyDev: Django

    Under the hood this will do a launch that will call python manage.py runserver (you could also create a custom launch which does that, PyDev just makes it a bit easier).

    And if you want a shell while dealing with Django you have to right-click the project and select Django > Shell with Django environment (this will give you a django shell but with support for code completion from PyDev).

    Note that code is not automatically added to that shell... if you change the code you need to restart the shell or manually reload the module you want (but given that django has a lot of magic it may be hard to do a reload).

    Note: you may want to take a look at http://www.pydev.org/manual_adv_django.html for more details on how to develop Django within PyDev.