Txing

欢迎来到 | 伽蓝之堂

0%

安装psycopg2库报错的处理方法

安装psycopg2库报错的处理方法

1. 问题背景

最近使用python处理PostgreSQL数据库相关的工作遇到报错,提示没装psycopg2库,看似问题不大,但处理起来还比较繁琐。

2. 解决方案

先尝试直接在虚拟环境中pip安装,执行

1
pip install psycopg2

结果提示安装失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Collecting psycopg2
Using cached psycopg2-2.9.5.tar.gz (384 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [25 lines of output]
/home/xxx/anaconda3/envs/apa/lib/python3.9/site-packages/setuptools/config/setupcfg.py:516: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running egg_info
creating /tmp/pip-pip-egg-info-w0g72i4h/psycopg2.egg-info
writing /tmp/pip-pip-egg-info-w0g72i4h/psycopg2.egg-info/PKG-INFO
writing dependency_links to /tmp/pip-pip-egg-info-w0g72i4h/psycopg2.egg-info/dependency_links.txt
writing top-level names to /tmp/pip-pip-egg-info-w0g72i4h/psycopg2.egg-info/top_level.txt
writing manifest file '/tmp/pip-pip-egg-info-w0g72i4h/psycopg2.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
<https://www.psycopg.org/docs/install.html>).

[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

根据报错提示,网上一股脑地说通过yum命令解决,执行yum install postgresql postgresql-devel python-devel python3-devel,但这显然是有特定语境的,不能直接照搬。yum(全称为 Yellow dog Updater, Modified )是一个在 Fedora 和 RedHat 以及SUSE 中的软件包管理器。而Ubuntu默认的包管理器为apt,使用yum可能会发生错误,不建议安装和使用。

因此使用apt的安装方式,执行

1
sudo apt-get install postgresql

遇到第二个报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
╰─> [40 lines of output]
/home/xxx/anaconda3/envs/apa/lib/python3.9/site-packages/setuptools/config/setupcfg.py:516: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-cpython-39
creating build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errors.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/__init__.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extras.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/sql.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extensions.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/tz.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/pool.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_range.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_json.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-39/psycopg2
running build_ext
building 'psycopg2._psycopg' extension
creating build/temp.linux-x86_64-cpython-39
creating build/temp.linux-x86_64-cpython-39/psycopg
gcc -pthread -B /home/xxx/anaconda3/envs/apa/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/xxx/anaconda3/envs/apa/include -fPIC -O2 -isystem /home/xxx/anaconda3/envs/apa/include -fPIC "-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=140007 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/xxx/anaconda3/envs/apa/include/python3.9 -I. -I/usr/include/postgresql -I/usr/include/postgresql/14/server -I/usr/include/libxml2 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-cpython-39/psycopg/adapter_asis.o -Wdeclaration-after-statement
In file included from psycopg/adapter_asis.c:28:
./psycopg/psycopg.h:36:10: fatal error: libpq-fe.h: 没有那个文件或目录
36 | #include <libpq-fe.h>
| ^~~~~~~~~~~~
compilation terminated.

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 '/usr/bin/gcc' 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
╰─> [42 lines of output]
/home/xxx/anaconda3/envs/apa/lib/python3.9/site-packages/setuptools/config/setupcfg.py:516: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running install
/home/xxx/anaconda3/envs/apa/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build
running build_py
creating build
creating build/lib.linux-x86_64-cpython-39
creating build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errors.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/__init__.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extras.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/sql.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/extensions.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/tz.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/pool.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_range.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_json.py -> build/lib.linux-x86_64-cpython-39/psycopg2
copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-39/psycopg2
running build_ext
building 'psycopg2._psycopg' extension
creating build/temp.linux-x86_64-cpython-39
creating build/temp.linux-x86_64-cpython-39/psycopg
gcc -pthread -B /home/xxx/anaconda3/envs/apa/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/xxx/anaconda3/envs/apa/include -fPIC -O2 -isystem /home/xxx/anaconda3/envs/apa/include -fPIC "-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=140007 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/xxx/anaconda3/envs/apa/include/python3.9 -I. -I/usr/include/postgresql -I/usr/include/postgresql/14/server -I/usr/include/libxml2 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-cpython-39/psycopg/adapter_asis.o -Wdeclaration-after-statement
In file included from psycopg/adapter_asis.c:28:
./psycopg/psycopg.h:36:10: fatal error: libpq-fe.h: 没有那个文件或目录
36 | #include <libpq-fe.h>
| ^~~~~~~~~~~~
compilation terminated.

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 '/usr/bin/gcc' 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.

继续安装:

1
sudo apt-get install libpq-dev

完成之后再安装psycopg2即可

1
pip install psycopg2

后续提示sqlalchemy版本过高,需要降低版本

1
pip install --upgrade sqlalchemy==1.4

问题解决~

2023年03月31日