苹果M1?安装psycopg2包符号未找到:_PQbackendPID
pg_config
venv ? which pg_config
/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
pip3 install -r requirements.txt
要求.txt
venv ? cat requirements.txt
-i https://mirrors.aliyun.com/pypi/simple/
alembic==1.4.2
amqp==2.6.0
billiard==3.6.3.0
celery==4.4.6
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
emoji==0.5.4
fastapi==0.59.0
future==0.18.2
h11==0.9.0
httptools==0.1.1 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
idna==2.10
kombu==4.6.11
mako==1.1.3
markupsafe==1.1.1
psycopg2-binary==2.8.5
pydantic==1.6
python-dateutil==2.8.1
python-editor==1.0.4
pytz==2020.1
redis==3.5.3
requests==2.24.0
six==1.15.0
sqlalchemy==1.3.18
starlette==0.13.4
tenacity==6.2.0
urllib3==1.25.9
uvicorn==0.11.5
uvloop==0.14.0 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
vine==1.3.0
websockets==8.1
完整的错误信息
Process SpawnProcess-1:
Traceback (most recent call last):
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/subprocess.py", line 62, in subprocess_started
target(sockets=sockets)
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/main.py", line 382, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/main.py", line 389, in serve
config.load()
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/config.py", line 288, in load
self.loaded_app = import_from_string(self.app)
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/importer.py", line 23, in import_from_string
raise exc from None
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./main.py", line 4, in <module>
from api.api_v1.api import api_router
File "./api/api_v1/api.py", line 4, in <module>
from api.api_v1.endpoints import status
File "./api/api_v1/endpoints/status.py", line 10, in <module>
from db.session import Session
File "./db/session.py", line 7, in <module>
engine = create_engine(config.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True)
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 753, in dbapi
import psycopg2
File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: dlopen(/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so, 2): Symbol not found: _PQbackendPID
Referenced from: /Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so
Expected in: flat namespace
in /Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so
回答
尝试通过自制软件安装 postgres:
brew install postgresql
出于某种原因,这似乎在正确的目录中安装了正确的库,以便您可以在 M1 Mac 上正确构建和导入 psycopg2-binary(无论如何在我的情况下工作)。
使用 brew 安装 postgres 后,卸载然后重新安装 psycopg2,例如:
pip3.9 install psycopg2-binary --force-reinstall --no-cache-dir
请注意,即使这将安装新版本的 postgres,您也不需要实际使用新版本来使 psycopg2 导入工作。
回答
Miniconda(virtualenv 的替代品)有一个 Python 3.8 安装程序,可与 M1 Mac 上的 psycopg2 一起使用,而不会出现 _PQbackendPID 错误:https ://docs.conda.io/en/latest/miniconda.html