Tony Ingle
Tony Ingle

Reputation: 587

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

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

Upvotes: 15

Views: 19553

Answers (5)

Luke Lyu
Luke Lyu

Reputation: 71

If you use env, ensure that your program is not using the globally installed Python interpreter at /usr/local/bin/python3. Instead, it should use the one within your virtual environment. You can verify this by running the which python command.

Upvotes: 0

Ashraf Gardizy
Ashraf Gardizy

Reputation: 389

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' } }

Upvotes: 3

Andrea Antoniazzi
Andrea Antoniazzi

Reputation: 49

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

pip install psycopg2-binary==2.9.3

Upvotes: 1

Leo
Leo

Reputation: 1068

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.

Upvotes: 4

Tony Ingle
Tony Ingle

Reputation: 587

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

Upvotes: 30

Related Questions