python3 postgreSQL 依赖问题

发布时间 2023-09-08 16:13:55作者: 郭小睿
  unable to execute 'gcc': No such file or directory
  
  It appears you are missing some prerequisite to build the package from source.
  
  You may install a binary package by installing 'psycopg2-binary' from PyPI.
  If you want to install psycopg2 from source, please install the packages
  required for the build and try again.
  
  For further information please check the 'doc/src/install.rst' file (also at
  <https://www.psycopg.org/docs/install.html>).
  
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for psycopg2
  Running setup.py clean for psycopg2

这个错误消息表明您在尝试安装 psycopg2 时出现了编译错误,因为缺少 gcc 编译器。psycopg2 是一个 PostgreSQL 数据库的 Python 客户端,它需要编译扩展模块才能正常工作。

解决此问题的方法是安装 gcc 编译器以及其他构建所需的依赖项。您可以使用以下命令来安装这些依赖项:

对于 Ubuntu/Debian 系统:

sudo apt-get update
sudo apt-get install gcc python3-dev libpq-dev

对于 CentOS/RHEL 系统:

sudo yum install gcc python3-devel postgresql-devel

一旦安装了这些依赖项,再次尝试安装 psycopg2

pip3 install psycopg2==2.9.7

如果您仍然遇到问题,可能需要检查是否存在其他缺失的依赖项或配置问题,具体取决于您的操作系统和环境。

另外,如果您只是需要使用 psycopg2 而不需要手动构建它,您可以考虑安装 psycopg2-binary 包,这是 psycopg2 的二进制版本,无需编译:

pip3 install psycopg2-binary==2.9.7

这通常更容易安装,并且不需要额外的编译步骤。