'rest_framework_nested.routers' error in Docker only
I am trying to deploy a Django app through docker but am stumped at this problem. When I run my application locally, I have no problems but when I deploy to Docker I get this error:
AttributeError: module 'rest_framework_nested.routers' has no attribute 'NestedDefaultRouter'
The last lines of the stack trace looks like this:
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/= /app/store/urls.py", line 13, in <module>
modules_router = routers.NestedDefaultRouter(router, 'modules', lookup='module')
AttributeError: module 'rest_framework_nested.routers' has no attribute 'NestedDefaultRouter'
The file where the error emanates from looks like this:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework_nested import routers
from . import views
router = DefaultRouter()
router.register('modules', views.ModuleViewSet, basename='modules')
modules_router = routers.NestedDefaultRouter(router, 'modules', lookup='module')
My Dockerfile looks as follows:
FROM python:3.9.9-slim
#Set up user
RUN apt-get update
RUN apt-get -y install sudo
RUN addgroup app && adduser -system app -ingroup app
USER app
WORKDIR = /app
#Environment settings`
ENV PYTHONUNBUFFERED=1
#Install MySQL and dependencies
RUN sudo apt-get -y install python3.9-dev
RUN sudo apt-get -y install default-libmysqlclient-dev
RUN sudo apt-get -y install build-essential libssl-dev libffi-dev
RUN sudo apt-get -y install libxml2-dev libxslt1-dev zlib1g-dev
RUN pip install mysqlclient
RUN sudo apt-get -y install default-mysql-server
RUN sudo apt-get -y install gunicorn3
#Copy files and install other dependencies
COPY . .
RUN pip3 install -r requirements.txt
I have been trawling the web for hours without luck. Seems to be a bit of an anomaly unless I am missing something very obvious.
Running the same code base same settings file etc. locally goes without issue. And, yes, drf-nested-routers==0.93.4
is in the requirements file. When I run pip3 install drf-nested-routers
in the docker environment I get that the requirement is already satisfied.
So I managed to track down this issue.
NestedDefaultRouter
is a method in rest_framework_nested
that comes with installing the drf-nested-routers
package. For some reason I also had the django-rest-framework-nested
package installed. This appears to have been the problem. After uninstalling django-rest-framework-nested
the image starts up without issues.
The fact that it worked locally and not in Docker is likely due an order of installation issue. Anyways, fixed now.