gpt4 book ai didi

python-3.x - pip install psycopg2 在 MacOS M1 和 python 3.10.5 上不起作用

转载 作者:行者123 更新时间:2023-12-02 18:06:58 26 4
gpt4 key购买 nike

psycopg2 无法在 M1 上运行。有人安装成功吗?

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_PQbackendPID'

我尝试安装:

  1. psycopg2
  2. psycopg2 和 psycopg2 二进制
  3. 仅 psycopg2-二进制

似乎没有什么效果。我使用的是python3.10.5

非常感谢!

最佳答案

从brew安装libpqopenssl:

  • brew 安装 libpq
  • brew 安装 openssl

检查路径和编译器标志:

>>> brew info libpq
libpq: stable 14.4 (bottled) [keg-only]
Postgres C API library
https://www.postgresql.org/docs/14/libpq.html
/opt/homebrew/Cellar/libpq/14.4 (2,338 files, 28.3MB)
Poured from bottle on 2022-07-20 at 16:31:04
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/libpq.rb
License: PostgreSQL
==> Dependencies
Required: krb5 ✔, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3acb3a6adb0b0af83f2edf2" rel="noreferrer noopener nofollow">[email protected]</a> ✔
==> Caveats
libpq is keg-only, which means it was not symlinked into /opt/homebrew,
because conflicts with postgres formula.

If you need to have libpq first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

For compilers to find libpq you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"

>>> brew info openssl
openssl@3: stable 3.0.5 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/opt/homebrew/Cellar/openssl@3/3.0.5 (6,444 files, 27.9MB)
Poured from bottle on 2022-07-20 at 16:31:21
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd3ccd9d2cfcfd0fc8f92cede" rel="noreferrer noopener nofollow">[email protected]</a>
License: Apache-2.0
==> Dependencies
Required: ca-certificates ✔
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/opt/homebrew/etc/openssl@3/certs

and run
/opt/homebrew/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.

If you need to have openssl@3 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

导出libpq的path并导出openssl的LD/CPPFLAGS(取自上面的输出)并安装psycopg2:

>>> export PATH=/opt/homebrew/opt/libpq/bin:$PATH
>>> export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
>>> export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
>>> python -m pip install psycopg2
Collecting psycopg2
Using cached psycopg2-2.9.3.tar.gz (380 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: psycopg2
Building wheel for psycopg2 (setup.py) ... done
Created wheel for psycopg2: filename=psycopg2-2.9.3-cp310-cp310-macosx_12_0_arm64.whl size=142252 sha256=78ca9fc7ca6752e234904bf38d052937e20b063cb68eb67caa874511207e076e
Stored in directory: /Users/.../Library/Caches/pip/wheels/81/b6/3d/091aad3e8919ea76c84c2674b02ce3ab52de882e091c39249e
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.3

测试:

python
Python 3.10.5 (main, Jul 20 2022, 17:05:05) [Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> c = psycopg2.connect("port=5556 host=localhost ...")
>>> c
<connection object at 0x102d8a500; dsn: 'user=xxx password=xxx dbname=xxx host=localhost port=5556', closed: 0>
>>> exit()

关于python-3.x - pip install psycopg2 在 MacOS M1 和 python 3.10.5 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73042760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com