解决 pip 安装 Python 包时出现的 gcc 错误

报错内容

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
# pip install advocate
WARNING: The directory '/home/calibre/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting advocate
Downloading advocate-1.0.0-py2.py3-none-any.whl (34 kB)
Requirement already satisfied: pyasn1 in /usr/lib/python3.9/site-packages (from advocate) (0.4.8)
Requirement already satisfied: pyopenssl in /usr/lib/python3.9/site-packages (from advocate) (21.0.0)
Collecting ndg-httpsclient
Downloading ndg_httpsclient-0.5.1-py3-none-any.whl (34 kB)
Requirement already satisfied: urllib3<2.0,>=1.22 in /usr/lib/python3.9/site-packages (from advocate) (1.25.11)
Requirement already satisfied: requests<3.0,>=2.18.0 in /usr/lib/python3.9/site-packages (from advocate) (2.24.0)
Requirement already satisfied: six in /usr/lib/python3.9/site-packages (from advocate) (1.15.0)
Collecting netifaces>=0.10.5
Downloading netifaces-0.11.0.tar.gz (30 kB)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/lib/python3.9/site-packages (from requests<3.0,>=2.18.0->advocate) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/lib/python3.9/site-packages (from requests<3.0,>=2.18.0->advocate) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3.9/site-packages (from requests<3.0,>=2.18.0->advocate) (2020.12.5)
Requirement already satisfied: cryptography>=3.3 in /usr/lib/python3.9/site-packages (from pyopenssl->advocate) (3.3.2)
Requirement already satisfied: cffi>=1.12 in /usr/lib/python3.9/site-packages (from cryptography>=3.3->pyopenssl->advocate) (1.14.5)
Requirement already satisfied: pycparser in /usr/lib/python3.9/site-packages (from cffi>=1.12->cryptography>=3.3->pyopenssl->advocate) (2.20)
Using legacy 'setup.py install' for netifaces, since package 'wheel' is not installed.
Installing collected packages: netifaces, ndg-httpsclient, advocate
Running setup.py install for netifaces ... error
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-2zyt2zvx/netifaces_257e61a61cd54520853e38d3a2eb6b6e/setup.py'"'"'; __file__='"'"'/tmp/pip-install-2zyt2zvx/netifaces_257e61a61cd54520853e38d3a2eb6b6e/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-eab0cpy3/install-record.txt --single-version-externally-managed --compile --install-headers /usr/include/python3.9/netifaces
cwd: /tmp/pip-install-2zyt2zvx/netifaces_257e61a61cd54520853e38d3a2eb6b6e/
Complete output (15 lines):
running install
running build
running build_ext
checking for getifaddrs...not found.
checking for getnameinfo...not found.
checking for socket IOCTLs...not found.
checking for optional header files...none found.
checking whether struct sockaddr has a length field...no.
checking which sockaddr_xxx structs are defined...none!
checking for routing socket support...no.
checking for sysctl(CTL_NET...) support...no.
checking for netlink support...no.
building 'netifaces' extension
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fomit-frame-pointer -g -fno-semantic-interposition -fomit-frame-pointer -g -fno-semantic-interposition -fomit-frame-pointer -g -fno-semantic-interposition -DTHREAD_STACK_SIZE=0x100000 -fPIC -DNETIFACES_VERSION=0.11.0 -I/usr/include/python3.9 -c netifaces.c -o build/temp.linux-x86_64-3.9/netifaces.o
error: command 'gcc' failed: No such file or directory
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-2zyt2zvx/netifaces_257e61a61cd54520853e38d3a2eb6b6e/setup.py'"'"'; __file__='"'"'/tmp/pip-install-2zyt2zvx/netifaces_257e61a61cd54520853e38d3a2eb6b6e/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-eab0cpy3/install-record.txt --single-version-externally-managed --compile --install-headers /usr/include/python3.9/netifaces Check the logs for full command output.

解决方案:

输入如下命令:

1
apk add --no-cache gcc musl-dev python3-dev libffi-dev openssl-dev