No matching distribution found for psycopg2-binary

在开发数据库应用时,使用pip在虚拟环境安装psycopg2时,常常会遇到以下问题:

Collecting psycopg2-binary==2.8.5
  Using cached psycopg2-binary-2.8.5.tar.gz (381 kB)
    ERROR: Command errored out with exit status 1:
     command: /work/wks_open/flask-restplus-boilerplate/venv/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-5sehxl8d/psycopg2-binary_279745a696d647eca48e99bcd851539b/setup.py'"'"'; __file__='"'"'/tmp/pip-install-5sehxl8d/psycopg2-binary_279745a696d647eca48e99bcd851539b/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-aj1acmpg
         cwd: /tmp/pip-install-5sehxl8d/psycopg2-binary_279745a696d647eca48e99bcd851539b/
    Complete output (23 lines):
    running egg_info
    creating /tmp/pip-pip-egg-info-aj1acmpg/psycopg2_binary.egg-info
    writing /tmp/pip-pip-egg-info-aj1acmpg/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to /tmp/pip-pip-egg-info-aj1acmpg/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to /tmp/pip-pip-egg-info-aj1acmpg/psycopg2_binary.egg-info/top_level.txt
    writing manifest file '/tmp/pip-pip-egg-info-aj1acmpg/psycopg2_binary.egg-info/SOURCES.txt'
    
    Error: pg_config executable not found.
    
    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.
    
    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.
    
    For further information please check the 'doc/src/install.rst' file (also at
    ).
    
    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/97/00/ed4c82364741031d745867f83820d4f373aa891098a5785841850491c9ba/psycopg2-binary-2.8.5.tar.gz#sha256=ccdc6a87f32b491129ada4b87a43b1895cf2c20fdb7f98ad979647506ffc41b6 (from https://pypi.org/simple/psycopg2-binary/) (requires-python:>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement psycopg2-binary==2.8.5 (from versions: 2.7.4, 2.7.5, 2.7.6, 2.7.6.1, 2.7.7, 2.8, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.8.5, 2.8.6, 2.9, 2.9.1)
ERROR: No matching distribution found for psycopg2-binary==2.8.5

网上的大部分解决方案一般为:

  1. 升级pip
python -m pip install --upgrade pip; 

2.更换pypi源
以清华的pypi源为例:

pip install -r requirements.txt -i -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple 

然而,经测试该包存在

curl https://files.pythonhosted.org/packages/97/00/ed4c82364741031d745867f83820d4f373aa891098a5785841850491c9ba/psycopg2-binary-2.8.5.tar.gz -o psycopg2-binary-2.8.5.tar.gz

向前排查,发现这样一句话 Error: pg_config executable not found.
经查阅资料
springmerchant & stackoverflow
原因在于 pg_config is in postgresql-devel开发库中(libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)

# 检查是否安装
$ sudo dnf list  libpq-devel python3-devel 
[sudo] password for brian: 
Last metadata expiration check: 0:14:49 ago on Thu 05 Aug 2021 21:25:36.
Installed Packages
python3-devel.x86_64                                         3.9.6-2.fc34                                         @updates
Available Packages
libpq-devel.i686                                             13.3-1.fc34                                          updates 
libpq-devel.x86_64                                           13.3-1.fc34                                          updates 
python3-devel.i686                                           3.9.6-2.fc34                                         updates 
# 安装libpq-devel 
sudo dnf install libpq-devel 

此时,再重新运行pip安装psycopg2-binary,就能够继续运行了。

另,在docker中,也需要编译安装psycopg2,如下:

FROM python:3.9.6-alpine3.14
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /src
WORKDIR /src
ADD . /src
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 5000

requirements.txt内容如下:

Click
Flask-Migrate
Flask-SQLAlchemy
Flask-Script
Flask
ItsDangerous
Jinja2
MarkupSafe==2.0.1
PyJWT
SQLAlchemy
Werkzeug
aniso8601
flask-restplus
jsonschema
marshmallow
psycopg2
pylint
python-dotenv
pytz
raven[flask]
six
uWSGI

你可能感兴趣的:(No matching distribution found for psycopg2-binary)