简体   繁体   中英

psycopg2 installed on M1 running MacOS Big Sur, but unable to run local server

I have the new (2021) Macbook Air running the Apple M1 chip. I have set up my django application, which uses postgresql, and I have created a virtualenv running Python 3.9.2. Have installed Python directly from the macOS 64-bit universal2 installer on python.org. After much difficulty, I was finally able to install psycopg2 using:

env LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/readline/lib" pip3 --no-cache install psycopg2-binary==2.8.6

I am able to SSH into my PostgreSQL DB, but when I run python manage.py runserver, I get the following error (abridged due to length).

File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/contrib
/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/contrib
/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/mode
ls/base.py", line 121, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/mode
ls/base.py", line 325, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/mode
ls/options.py", line 208, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/__in
it__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/util
s.py", line 207, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/util
s.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__
init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/django/db/back
ends/postgresql/base.py", line 29, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/
Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/psycopg2/_psycopg.cpyth
on-39-darwin.so, 2): Symbol not found: _PQbackendPID
  Referenced from: /Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/psy
copg2/_psycopg.cpython-39-darwin.so
  Expected in: flat namespace
 in /Users/seb/.virtualenvs/onefabric/lib/python3.9/site-packages/psycopg2/_psycopg.
cpython-39-darwin.so

How can I resolve this and run my local server?

This worked for me: https://github.com/psycopg/psycopg2/issues/1208#issuecomment-768594079

I removed the virtualenv and recreated it running

python3 -m venv ~/.virtualenvs/onefabric

inside the Atom's terminal

This helped me:

  1. Download last universal2 or arm version of Python from https://www.python.org/downloads/macos/ and install it.
  2. Recreate virtualenv: python3 -m venv /your/path/to/venv/ then activate.
  3. Install psycopg2: pip install psycopg2 --no-cache-dir
  4. Install psycopg2-binary: pip install psycopg2-binary --no-cache-dir

Good luck!

Installing libpq from source got things working again. Of course, do this in the Rosetta terminal.

$ brew install libpq --build-from-source

$ export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"

$ pip install psycopg2

Courtesy: https://github.com/psycopg/psycopg2/issues/1216#issuecomment-767892042

Extras: If it still doesn't work, try

brew install openssl

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

then install psycopg2

pip3 install psycopg2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM