gpt4 book ai didi

python - 配置不当的 : Error loading psycopg2 module: No module named 'psycopg2._psycopg' zappa

转载 作者:行者123 更新时间:2023-12-04 03:26:13 24 4
gpt4 key购买 nike

我正在使用 zappa 将后端部署到 AWS Lambda。它运行良好,直到我决定使用 PostgreSQL。我在这样的设置中添加了它:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('DATABASE_NAME'),
'USER': config('DATABASE_USER'),
'PASSWORD': config('DATABASE_PASSWORD'),
'HOST': config('DATABASE_HOST'),
'PORT': '5432'
}
}
我正在使用 AWS RDS。我安装了 psycopg2-binary 和 psycopg2(版本 2.8.6),但问题仍然存在。 python版本是3.8。
完整的错误日志:
   [1621168086542] [ERROR] ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2._psycopg'
Traceback (most recent call last):
  File "/var/task/handler.py", line 609, 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 146, in __init__
    wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
  File "/var/task/zappa/ext/django_zappa.py", line 20, in get_django_wsgi
    return get_wsgi_application()
  File "/var/task/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/var/task/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/var/task/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/var/task/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/var/task/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/var/task/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/var/task/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/var/task/django/db/models/options.py", line 206, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/var/task/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/var/task/django/db/utils.py", line 214, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/var/task/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/var/task/django/db/backends/postgresql/base.py", line 29, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)

最佳答案

(可能的)问题是您正在 MacOS 上构建 lambda .zip 包。当您部署 lambda 函数时,它在 Linux 环境(特别是 AWS 的 Linux2 环境)中运行。 MacOS 和 Linux 环境的 psycopg2-binary 不同,因此如果您在 Mac 上构建 lambda 包(包括 psycopg2-binary ),然后部署到 lambda,您将遇到上述问题。
您需要在 AWS Linux 容器内构建 lambda 函数。这是一个 Dockerfile,可用于创建一个容器,在其中安装 psycopg2-binary 并构建 lambda zip 包。那么一切都应该有效:

FROM amazonlinux:2.0.20200207.1

RUN cd /opt && \
yum install -y gcc openssl-devel bzip2-devel libffi-devel wget tar gzip make && \
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz && \
tar xzf Python-3.8.2.tgz && \
cd Python-3.8.2 && \
./configure --enable-optimizations && \
make altinstall && \
rm -f /opt/Python-3.8.2.tgz && \
echo "alias python3=python3.8" > ~/.bashrc
请注意 amaonzonlinux:2.0 操作系统,然后我只将 python 3.8.2 安装到环境中(如果需要,您可以使用不同版本的 python)。从那里你可以复制你的代码并构建你的 lambda .zip 包并部署到 lambda。

关于python - 配置不当的 : Error loading psycopg2 module: No module named 'psycopg2._psycopg' zappa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67556906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com