1

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
  • 45
  • 6

1 Answers1

2

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
  • 45
  • 6
  • 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