Zappa icon indicating copy to clipboard operation
Zappa copied to clipboard

ModuleNotFoundError: No module named 'base' in nested Docker Zappa deployment

Open Destaq opened this issue 4 weeks ago • 0 comments

Context

I'm trying to locally test a Zappa deployment that is made using Docker, following these instructions. However, despite things running locally without Zappa, on my local docker server I keep getting ModuleNotFoundError: No module named 'base'.

Project structure locally, can't seem to access base from Zappa (or api presumably):

server
  api (folder)
  base (folder)
  server (folder)
    settings.py
    urls.py
    wsgi.pi
    ...
  manage.py
  setup.py
  ...

Expected Behavior

Able to run Zappa and access my Django API through Docker.

Actual Behavior

2024-06-04 20:58:31 LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
[ERROR] ModuleNotFoundError: No module named 'base'
Traceback (most recent call last):
  File "/var/task/handler.py", line 657, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 241, in lambda_handler
    handler = global_handler or cls()
  File "/var/task/handler.py", line 138, in __init__
    self.app_module = importlib.import_module(self.settings.APP_MODULE)
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/tmp/backend/server/server/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/tmp/backend/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/tmp/backend/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/tmp/backend/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/tmp/backend/django/apps/config.py", line 193, in create
    import_module(entry)
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked

Possible Fix

Tried changing base to server.base to no avail, played around with both options for ROOT_URLCONF to no avail.

WSGI_APPLICATION = "server.wsgi.application"
ROOT_URLCONF = "server.urls"

# WSGI_APPLICATION = 'server.server.wsgi.application'
# ROOT_URLCONF = 'server.server.urls'

Steps to Reproduce

Please see the environment, below.

Your Environment

  • Zappa version used: 0.58.0
  • Operating System and Python version: MacOS Ventura Apple Silicon, Python 3.11

For reference, here is my Docker file:

# Build from AWS's Python 3.11 image
ARG PYTHON_BASE_VERSION=3.11
FROM public.ecr.aws/lambda/python:${PYTHON_BASE_VERSION}

# Copy Django project files
ARG FUNCTION_DIR="/var/task"

COPY ./ ${FUNCTION_DIR}

# Install dependencies
RUN python3 -m pip install --upgrade pip && \
    python3 -m pip install --no-cache-dir -r ${FUNCTION_DIR}/requirements.txt

# Grab the zappa handler.py and put it in the working directory
RUN ZAPPA_HANDLER_PATH=$( \
    python -c "from zappa import handler; print (handler.__file__)" \
    ) \
    && echo $ZAPPA_HANDLER_PATH \
    && cp $ZAPPA_HANDLER_PATH ${FUNCTION_DIR}/

CMD [ "handler.lambda_handler" ] 

Here is settings_zappa.json:

{
    "production": {
        "app_function": "server.server.wsgi.application",
        "django_settings": "server.server.settings",
        "project_name": "backend",
        "aws_region": "us-east-1",
        "runtime": "python3.11",
        "s3_bucket": "docker-zappa-bucket",
        "cors": true,
        "timeout": 1200,
        "slim_handler": true,
        "lambda_description": "Zappa + Docker + Django"
    }
}

This is where the issue is coming up in server/server/settings.py.

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_extensions",
    "rest_framework",
    "corsheaders",
    "rest_framework_simplejwt",
    "sslserver",
    "storages",
    "base",
    "api",
    # 'base.models.User'
]

Output of pip freeze:

annotated-types==0.6.0
anyio==4.2.0
api==0.0.7
argcomplete==3.2.2
asgiref==3.7.2
awslambdaric==2.0.10
boto3==1.34.29
botocore==1.34.29
certifi==2023.11.17
cffi==1.16.0
cfn-flip==1.3.0
charset-normalizer==3.3.2
click==8.1.7
cryptography==42.0.2
decorator==4.4.2
distro==1.9.0
Django==4.2.9
django-cors-headers==4.3.1
django-environ==0.11.2
django-extensions==3.2.3
django-sslserver==0.22
django-storages==1.14.2
djangorestframework==3.14.0
djangorestframework-simplejwt==5.3.1
docker==7.0.0
durationpy==0.6
exceptiongroup==1.2.0
filelock==3.13.1
fsspec==2024.2.0
h11==0.14.0
hjson==3.1.0
httpcore==1.0.2
httpx==0.26.0
idna==3.6
imageio==2.34.0
imageio-ffmpeg==0.4.9
jmespath==1.0.1
kappa==0.6.0
MarkupSafe==2.1.4
moviepy==1.0.3
networkx==3.2.1
nose==1.3.7
numpy==1.26.4
openai==1.12.0
packaging==23.2
pillow==10.2.0
placebo==0.9.0
proglog==0.1.10
psycopg2-binary==2.9.9
pycparser==2.21
pydantic==2.6.1
pydantic_core==2.16.2
PyJWT==2.8.0
pyOpenSSL==24.0.0
PyPDF2==3.0.1
python-dateutil==2.8.2
python-slugify==8.0.4
pytz==2023.3.post1
PyYAML==6.0.1
regex==2023.12.25
requests==2.31.0
s3transfer==0.10.0
simplejson==3.17.2
six==1.16.0
sniffio==1.3.0
SpeechRecognition==3.10.1
sqlparse==0.4.4
text-unidecode==1.3
toml==0.10.2
tqdm==4.66.1
troposphere==4.5.3
typing_extensions==4.9.0
urllib3==1.26.18
Werkzeug==3.0.1
zappa==0.58.0

Our student team has spent considerable time trying to solve this, any advice would be greatly appreciated.

Destaq avatar Jun 04 '24 19:06 Destaq