在 venv、Windows 10 中使用 pip 安装 psycopg2 时遗留安装失败

问题描述 投票:0回答:2

点子版本:22.3.1
Python版本:3.9.11
操作系统:Windows 10
我创建了我的 python 项目并使用 venv 作为虚拟环境。
其他pip包都安装好了,但只有psycopg2失败了。
日志说有问题
./psycopg/win32_support.h:43:6:错误:#elif 没有表达式。
我认为 msys2 可能会导致这个问题。

pip install psycopg2-binary
也有同样的结果,同样的问题。
为什么会出现这种情况?有线索吗?

(.venv) PS C:\Users\my\proj> pip install psycopg2
Collecting psycopg2
  Using cached psycopg2-2.9.5.tar.gz (384 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [44 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.mingw_x86_64-3.9
      creating build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\errorcodes.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\errors.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\extensions.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\extras.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\pool.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\sql.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\tz.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\_ipaddress.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\_json.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\_range.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\__init__.py -> build\lib.mingw_x86_64-3.9\psycopg2
      running build_ext
      building 'psycopg2._psycopg' extension
      creating build\temp.mingw_x86_64-3.9
      creating build\temp.mingw_x86_64-3.9\psycopg
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O2 -pipe -march=x86-64 -mtune=generic -O2 -pipe -DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64) -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=150001 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -IC:\Users\my\proj\.venv\include -IC:\msys64\mingw64\include\python3.9 -I. -IC:/PROGRA~1/POSTGR~1/15/include -IC:/PROGRA~1/POSTGR~1/15/include/server -c psycopg\adapter_asis.c -o build\temp.mingw_x86_64-3.9\psycopg\adapter_asis.o -O3 -fno-strict-aliasing
      In file included from ./psycopg/config.h:164,
                       from ./psycopg/psycopg.h:38,
                       from psycopg\adapter_asis.c:28:
      ./psycopg/win32_support.h:43:6: error: #elif with no expression
         43 | #elif
            |      ^
      In file included from ./psycopg/psycopg.h:38,
                       from psycopg\adapter_asis.c:28:
      ./psycopg/config.h:108:12: warning: 'pthread_mutex_init' defined but not used [-Wunused-function]
        108 | static int pthread_mutex_init(pthread_mutex_t *mutex, void* fake)
            |            ^~~~~~~~~~~~~~~~~~
     
      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 'C:\\msys64\\mingw64\\bin\\gcc.exe' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for psycopg2
  Running setup.py clean for psycopg2
Failed to build psycopg2
Installing collected packages: psycopg2
  Running setup.py install for psycopg2 ... error
  error: subprocess-exited-with-error

  × Running setup.py install for psycopg2 did not run successfully.
  │ exit code: 1
  ╰─> [44 lines of output]
      running install
      running build
      running build_py
      creating build
      creating build\lib.mingw_x86_64-3.9
      creating build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\errorcodes.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\errors.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\extensions.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\extras.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\pool.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\sql.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\tz.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\_ipaddress.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\_json.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\_range.py -> build\lib.mingw_x86_64-3.9\psycopg2
      copying lib\__init__.py -> build\lib.mingw_x86_64-3.9\psycopg2
      running build_ext
      building 'psycopg2._psycopg' extension
      creating build\temp.mingw_x86_64-3.9
      creating build\temp.mingw_x86_64-3.9\psycopg
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O2 -pipe -march=x86-64 -mtune=generic -O2 -pipe -DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64) -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=150001 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -IC:\Users\my\proj\.venv\include -IC:\msys64\mingw64\include\python3.9 -I. -IC:/PROGRA~1/POSTGR~1/15/include -IC:/PROGRA~1/POSTGR~1/15/include/server -c psycopg\adapter_asis.c -o build\temp.mingw_x86_64-3.9\psycopg\adapter_asis.o -O3 -fno-strict-aliasing
      In file included from ./psycopg/config.h:164,
                       from ./psycopg/psycopg.h:38,
                       from psycopg\adapter_asis.c:28:
      ./psycopg/win32_support.h:43:6: error: #elif with no expression
         43 | #elif
            |      ^
      In file included from ./psycopg/psycopg.h:38,
                       from psycopg\adapter_asis.c:28:
      ./psycopg/config.h:108:12: warning: 'pthread_mutex_init' defined but not used [-Wunused-function]
        108 | static int pthread_mutex_init(pthread_mutex_t *mutex, void* fake)
            |            ^~~~~~~~~~~~~~~~~~
     
      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 'C:\\msys64\\mingw64\\bin\\gcc.exe' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> psycopg2

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.
pip psycopg2
2个回答
1
投票

尝试以下命令 它对我有用

sudo apt-get update

sudo apt-get -y install python3-pip

sudo apt install libpq-dev python3-dev

pip3 install psycopg2

0
投票

就我而言,我能够通过首先安装软件包来解决问题: 1) pip 安装 psycopg2-二进制文件 进而 2)安装: pip 安装 psycopg2
对于错误:

收集psycopg2 使用缓存的 psycopg2-2.9.9.tar.gz (384 kB) 准备元数据(setup.py)...错误 错误:子进程退出并出现错误

× python setup.py Egg_info 没有运行成功。 │ 退出代码:1

© www.soinside.com 2019 - 2024. All rights reserved.