【问题标题】:ImproperlyConfigured("Error loading psycopg2 module: %s" % e)ImproperlyConfigured("加载 psycopg2 模块时出错:%s" % e)
【发布时间】:2022-01-09 01:09:41
【问题描述】:

目前在 MacOS Monterey 上使用 Django 4.0 和 Python 3.10.0。运行命令后

python3 manage.py runserver

我收到此错误

ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/tonyingle/.local/share/virtualenvs/total-weather-backend-nYZrqAi-/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_PQbackendPID'

我已经在 pipenv shell 及其外部安装了 pyscopg2 和 psycog2-binary。奇怪的是,一切正常,直到我意识到我必须配置我的 settings.py 文件来解决我的 cors 问题,然后一切都变得混乱了。

可能是settings.py中的一些相关设置

ALLOWED_HOSTS = [
    "https://total-weather-backend.herokuapp.com/", "http://127.0.0.1:8000/"]

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api',
    'rest_framework',
    'djoser',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
         ...
]

CORS_ORIGIN_ALLOW_ALL = True

【问题讨论】:

    标签: python django psycopg2


    【解决方案1】:

    当然,我在发布问题后马上就知道了,但这是我所做的对我有用的事情。

    brew install postgresql
    
    pip install psycopg2-binary --force-reinstall --no-cache-dir
    

    Apple M1: install psycopg2 package Symbol not found: _PQbackendPID

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 2019-09-29
    • 2014-04-22
    • 2021-08-05
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多