ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
Asked Answered
E

4

14

Currently on MacOS Monterey working with Django 4.0 and Python 3.10.0. After running the command

python3 manage.py runserver

I get this error

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'

I have already installed pyscopg2 and psycog2-binary in the pipenv shell and outside of it. The weird part about it is that everything worked fine until I realized I had to configure my settings.py file to fix my cors issue and then everything went haywire.

Maybe some relevant settings in 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
En answered 9/1, 2022 at 1:9 Comment(0)
E
30

Of course I figure it out right after I post the question, but here is what I did that worked for me.

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

Apple M1: install psycopg2 package Symbol not found: _PQbackendPID

En answered 9/1, 2022 at 1:17 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Shell
For context, this seems to be an issue that is bound to arise when you first start using Postgresql with Django. psycopg2 is not installed with Django by default, so you need to install it manually.Goglet
F
4

I was running psycopg2 version 2.9.1. I updated it to the current latest version of 2.9.4, and it worked. I used

pip install psycopg2 -U

where -U is for updating the package. I did this in my python virtual environment. I also edited requirements.txt to bump the version, psycopg2==2.9.4, just to be safe.

Fatidic answered 21/10, 2022 at 1:42 Comment(0)
D
2

Simple Solution:

You also need to install the binary file as well. I had the same problem and solved by running the following commands:

  1. Install pscycopy:

    pip install psycopg

  2. Install the binary files:

    python3 -m pip install psycopg-binary==3.1.13

  3. settings.py file:

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME':'employeeDB', 'USER':'postgres', 'PASSWORD':'postgres', 'HOST':'localhost' } }

Degauss answered 19/11, 2023 at 12:10 Comment(0)
B
1

I solved by downgrading to a previous version (current latest is 2.9.6)

pip install psycopg2-binary==2.9.3

Blowzy answered 25/5, 2023 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.