Home > front end >  Apple M1 run Django display symbol not found in flat namespace '_PQbackendPID'
Apple M1 run Django display symbol not found in flat namespace '_PQbackendPID'

Time:09-12

I am use django to build the project, when I run python manage.py makemigrations it get the error message:

Traceback (most recent call last):
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    import psycopg2 as Database
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_PQbackendPID'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mingzhe/Desktop/webapp/electric/electric/manage.py", line 22, in <module>
    main()
  File "/Users/mingzhe/Desktop/webapp/electric/electric/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/models/base.py", line 141, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/models/base.py", line 369, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/models/options.py", line 231, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 28, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/mingzhe/Desktop/webapp/electric/env/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_PQbackendPID'

I already create virtualenv here is my step:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew update
$ brew install postgresql
$ brew services start postgresql

$ virtualenv env
$ source env/bin/activate
(env) $ pip install django psycopg2-binary

I has been used most people's solution: install psycopg2-binary not psycopg2 but still get symbol not found in flat namespace '_PQbackendPID'

My system environment:

macOS Monterey
Python3.10.7 64bit
asgiref==3.5.2
Django==4.1.1
psycopg2-binary==2.9.3
sqlparse==0.4.2

I has try to brew uninstall postgresql and install again it still have same issue, I'm afraid I'm doing something wrong or maybe I don't get how it work on Macbook M1.

Any help or explanation is welcome! Thank you.

CodePudding user response:

try this pip3 install psycopg2-binary --no-cache-dir

CodePudding user response:

 pip uninstall psycopg2

 pip install psycopg2-binary --no-cache-dir
  • Related