【问题标题】:zappa handler not import django appzappa 处理程序不导入 django 应用程序
【发布时间】:2018-08-09 00:54:13
【问题描述】:

我在 zappa 的帮助下部署了 aws lambda django 应用程序。我的项目在本地使用 wsgi 运行良好,但是当我在 aws lambda 上部署时,它会引发错误。

有什么帮助吗?

扎帕尾巴:

:`Calling tail for stage new2..
Warning! AWS Lambda may not be available in this AWS Region!
Warning! AWS API Gateway may not be available in this AWS Region!
[1496566100097] [INFO] 2017-06-04T08:48:20.97Z 8e08e84e-4902-11e7-9744-b7104a9a6ab2 Detected environment to be AWS Lambda. Using synchronous HTTP transport.
[1496566100160] No module named accounts: ImportError
Traceback (most recent call last):
  File "/var/task/handler.py", line 484, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 240, in lambda_handler
  handler = cls()
  File "/var/task/handler.py", line 143, in __init__
  wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
  File "/var/task/django_zappa_app.py", line 20, in get_django_wsgi
  return get_wsgi_application()
  File "/tmp/pip-build-dt_DVN/Django/django/core/wsgi.py", line 13, in get_wsgi_application
  File "/tmp/pip-build-dt_DVN/Django/django/__init__.py", line 27, in setup
  File "/tmp/pip-build-dt_DVN/Django/django/apps/registry.py", line 85, in populate
  File "/tmp/pip-build-dt_DVN/Django/django/apps/config.py", line 94, in create
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
  __import__(name)
ImportError: No module named accounts
[1496566112758] 'NoneType' object is not callable

` 我的项目树:

    .
├── apps
│   ├── accounts
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── filters.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── serializers.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── helpers
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── __init__.py
│ 
├── manage.py
├── my_project
│   ├── apiv1_urls.py
│   ├── __init__.py
│   ├── partner_logging.py
│   ├── settings
│   │   ├── base.py
│   │   ├── conf.ini
│   │   
│   │   ├── dev.py
│   │   ├── __init__.py
│   │   └── prod.py
│   ├── urls.py
│   └── wsgi.py
├── requirements
│   ├── base.txt
│   └── dev.txt
└── zappa_settings.json

zappa_settings.json

{
"new2": {
    "aws_region": "ap-south-1",
    "django_settings": "my_project.settings",
    "profile_name": "default",
    "s3_bucket": "xyz"
}

}

wsgi.py

import os
import sys
from django.core.wsgi import get_wsgi_application
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")

application = get_wsgi_application()

【问题讨论】:

  • 这里有同样的问题。如果您检查 zip 文件,您可能会发现应用程序文件夹丢失。我仍在研究如何让 Zappa 解决这个问题,到目前为止,include 设置没有帮助。

标签: python django amazon-web-services aws-lambda


【解决方案1】:

我的问题原来是我的应用程序和我的虚拟环境具有相同的名称。因此,如果您使用 virtualenvwrapper 并且您的 venv 名为 apps 或 accounts,请尝试使用不同的名称创建一个新的。

zappa package $ENV -o zappa.zip && unzip -vl zappa.zip 如果您想查看它实际上传的内容

https://github.com/Miserlou/Zappa#package

$ zappa -v
0.46.1
$ zappa package -h
usage: zappa package [-h] [--all] [-a APP_FUNCTION] [-s SETTINGS_FILE] [-q]
                     [-j] [--disable_progress] [-o OUTPUT]
                     [stage_env]

positional arguments:
  stage_env

【讨论】:

猜你喜欢
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
相关资源
最近更新 更多