8

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
Tony Ingle
  • 467
  • 4
  • 10

3 Answers3

25

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

Tony Ingle
  • 467
  • 4
  • 10
  • 1
    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](/help/how-to-answer). – Community Jan 09 '22 at 06:50
  • 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. – Leland Reardon May 20 '23 at 11:28
3

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.

Diego
  • 867
  • 7
  • 13
0

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

pip install psycopg2-binary==2.9.3