Error (AppRegistryNotReady) running test cases with Django inside a Docker container

I read a similar solutions posted here:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360002753800-Django-console-not-working-properly-on-docker

And I have implemented the above solution.  The strange thing is I can open a Django Console in PyCharm without error.  And I've confirmed that the Django Console is running inside my Docker container by executing the following:

```python

>>> import socket
>>> socket.gethostname()
'f6f418ce5d14'

```

I have an entry point script (bash script), which executes the following inside the Docker container without error:

```

python manage.py migrate
python manage.py loaddata --format json /srv/app/api/opp/management/fixtures/dev.json

```

But, when PyCharm attempts to execute the test cases, using PyCharm's own nose test runner, that's when the error appear.  The following is an excerpt from PyCharm's output when running the Django test cases using a Docker container:

```

app_1 | Installed 127 object(s) from 1 fixture(s)
app_1 | app_1 | Launching Nosetest with arguments /opt/.pycharm_helpers/pycharm/_jb_nosetest_runner.py /srv/app/api/opp/tests in /srv/app/api
app_1 |
app_1 | app_1 | app_1 | app_1 | app_1 | app_1 | app_1 | app_1 |
error in setup context
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/nose/suite.py", line 210, in run
self.setUp()
File "/usr/local/lib/python3.8/site-packages/nose/suite.py", line 293, in setUp
self.setupContext(ancestor)
File "/usr/local/lib/python3.8/site-packages/nose/suite.py", line 316, in setupContext
try_run(context, names)
File "/usr/local/lib/python3.8/site-packages/nose/util.py", line 471, in try_run
return func()
File "/usr/local/lib/python3.8/site-packages/django/test/testcases.py", line 1131, in setUpClass
call_command('loaddata', *cls.fixtures, **{'verbosity': 0, 'database': db_name})
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 102, in call_command
app_name = get_commands()[command_name]
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 68, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 144, in get_app_configs
self.check_apps_ready()
File "/usr/local/lib/python3.8/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.

```

It seems like it is the test runner provided by PyCharm, which is causing the issue.  Based on the output I'm seeing, I believe the PyCharm test runner is not executing the following commands before running the test cases:

```

import django

django.setup()

```

I have tried to find the PyCharm test running inside the Docker container, so that I might manually edit it.  But, I cannot find it.  Help me Obi Wan Kanobi, you're my only hope!  

0
2 comments

Do you use docker-compose in PyCharm?
Have you configured it following the tutorial? https://www.jetbrains.com/help/pycharm/using-docker-compose-as-a-remote-interpreter.html

0
Avatar
Permanently deleted user

Hi Sergey, thanks for the reply.  Yes, I do use docker-compose in PyCharm.  And I did follow the tutorial you posted, but it didn't seem to fix my issue.  Coincidentally enough, today I went through my Dockerfile and docker-compose.yaml files line by line.  I found there were a few issues completely unrelated to Docker and PyCharm.  I had copied in some other Bash config scripts from a collegue.  It turns out the problem was issues with not having all of the right alpine packages installed.  And have two extra spaces when setting a Bash variable.  For example, instead of this wrong line:

VARIABLE = "some value"

I should have had:

VARIABLE = "some value"

Needless to say, I feel a bit foolish to have missed something so trivial, but at the time I did not yet know how to troubleshoot Docker issues inside of an python:3-alpine image.  Thanks again for the suggestion.  Have a good day!

0

Please sign in to leave a comment.