Another Django "Apps aren't loaded yet"

django models
django documentation
django model fields
django tutorial
django foreign key
django queryset
django admin
django orm

I'm very new to both django and python and I have run in too an error to which I do not understand. The runserver command works, and I can enter my website on localhost. But the Shell is broken somehow. Im using PyCharm on an Conda virtual env as far as I know. I'm using mac os high sierra. Edit: Also using Postgres.

Error when trying to import a model

Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from catalog.models import Routines
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/fridalarsson/PycharmProjects/hapionline6/catalog/models.py", line 3, in <module>
    from senders.models import Senders
  File "/Users/fridalarsson/PycharmProjects/hapionline6/senders/models.py", line 4, in <module>
    class Senders(models.Model):
  File "/Users/fridalarsson/anaconda3/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/fridalarsson/anaconda3/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/fridalarsson/anaconda3/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

The model in question looks like this:

from __future__ import unicode_literals
from django.db import models
from senders.models import Senders


class Routines(models.Model):
    routine_id = models.AutoField(primary_key=True)
    sender_id = models.ForeignKey(Senders, on_delete=models.CASCADE, default=1)
    in_routine = models.CharField(max_length=20)
    out_routine = models.CharField(max_length=20)
    mail_class = models.CharField(max_length=5)
    last_change = models.DateTimeField(auto_now=True)
    def __int__(self):
        return self.routine_id

Another weird this is that when if I instead of the regular "mac os" terminal use the PyCharm shell it starts with an error message like this:

/Users/fridalarsson/.conda/envs/hapionline6/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 63555 63556
import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend(['/Users/fridalarsson/PycharmProjects/hapionline6', '/Applications/PyCharm.app/Contents/helpers/pycharm', '/Applications/PyCharm.app/Contents/helpers/pydev'])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run("/Users/fridalarsson/PycharmProjects/hapionline6")
PyDev console: starting.
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Django 2.0.5
Traceback (most recent call last):
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
    import psycopg2 as Database
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 19, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 6, in <module>
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 19, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/models/base.py", line 114, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/models/base.py", line 315, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/models/options.py", line 205, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

If anyone could help I'd be grateful!

I ran into this problem. The problem for me was that psycopg2 and six were clashing. I was using psycopg2==2.6.1 and mismatched version of six. I specified to use six 1.10 and psycopg2==2.7.1 and it worked. I would verify that your version of six works with the verision of psycopg2. If that doesn't work I would take a look at this. If you are not sure how to check your versions, write out a requirements.txt using pip freeze > requirements.txt .

Related objects reference | Django documentation, The “other side” of a ForeignKey relation. field can't be set to None ( NULL ), then an object can't be removed from a relation without being added to another. Django Unchained tells the story of Django (Jamie Foxx), a slave who is soon picked up by bounty hunter Dr King Shultz (Christoph Waltz). The story follows on as Shultz takes on Django as his "deputy" during their tasks of bounty hunting, in return Shultz says that after winter he will help find Django's lost wife, Broomhilda.

Solved: As Josewails pointed out. The error was indeed psycopg2 related.

Since I'm using Conda virtual env, I was able to install psycopg2 through the "Anaconda navigator" gui/application.

It then added the dependent libpg etc.

Now it everything works both in the terminal and "runserver".

Writing your first Django app, part 3 | Django documentation, In Django, web pages and other content are delivered by views. Within the templates directory you have just created, create another directory called polls  Create a Python file containing the required functions. Create another Python file and import the previous Python file into it. Call the functions defined in the imported file. The above approach has been used in the below examples: Example 1: A Python file test.py is created and it contains the displayText() function.

Another solution was to reinstall psycopg with.

pip install --global-option=build_ext \
            --global-option="-I/usr/local/opt/openssl/include" \
            --global-option="-L/usr/local/opt/openssl/lib" psycopg2

For some reason this worked

Writing custom model fields | Django documentation, This is another case where a Field subclass will help you use your object with and retrieve from the hand attribute in our model just like any other Python class. One inspiration for the film is Corbucci's 1966 Spaghetti Western Django, whose star Franco Nero has a cameo appearance in Django Unchained. Another inspiration is the 1975 film Mandingo, about a slave trained to fight other slaves. Tarantino included scenes in the snow as a homage to The Great Silence. "Silenzio takes place in the snow.

Model class reference | Django documentation, Consider the following example: from django.db import models class Person(​models.Model): # Add manager with another name people = models.Manager(). Managing to use data from one Django application in another but the urls are different. Related. 4553. Calling an external command from Python. 1656.

Advanced tutorial: How to write reusable apps, We'll be turning our Web-poll into a standalone Python package you can reuse in new projects and share with other people. If you haven't recently completed  Another common method to include in Django models is get_absolute_url(), which returns a URL for displaying individual model records on the website (if you define this method then Django will automatically add a "View on Site" button to the model's record editing screens in the Admin site).

Overriding templates | Django documentation, In your project, you might want to override a template in another Django application, whether it be a In other words, DIRS is searched before APP_DIRS . Models¶ A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. The basics: Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database

Comments
  • Are you using PostgreSQL? If yes, you should have psycopg2 installed. It is the Python driver for PostgreSQL.
  • Hello and thanks. Yes I'm using Postgres and I have installed psycopg2 to my knowledge. If I run "pip install psycopg2" it says "all requirements met" or something and the same if i run "conda install psycopg2. So in the last "error-message" it cant find it for some reason.
  • definitely something with psycopg2. Created a new project with postgres. same as last above error occurs.