Python:pip 安装第三方包时报 ModuleNotFoundError: No module named '_ctypes' 错误

环境

CentOS 8.2

问题

pip install XXX 时遇到报错 ModuleNotFoundError: No module named '_ctypes'

报错:

[root@ebdedabb5237 tmp]# pip install bson
Collecting bson
  Using cached bson-0.5.10.tar.gz (10 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-233poybl/bson/setup.py'"'"'; __file__='"'"'/tmp/pip-install-233poybl/bson/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-hdalk4dk
         cwd: /tmp/pip-install-233poybl/bson/
    Complete output (11 lines):
    Traceback (most recent call last):
      File "", line 1, in 
      File "/usr/local/lib/python3.8/site-packages/setuptools/__init__.py", line 18, in 
        from setuptools.dist import Distribution
      File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 32, in 
        from setuptools import windows_support
      File "/usr/local/lib/python3.8/site-packages/setuptools/windows_support.py", line 2, in 
        import ctypes
      File "/usr/local/lib/python3.8/ctypes/__init__.py", line 7, in 
        from _ctypes import Union, Structure, Array
    ModuleNotFoundError: No module named '_ctypes'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

原因

Python3 中有个内置模块叫 ctypes,它是 Python3 的外部函数库模块,它提供兼容 C 语言的数据类型,并通过它调用Linux系统下的共享库 (Shared library),此模块需要使用 CentOS 系统中外部函数库 (Foreign function library) 的开发链接库(头文件和链接库)。
由于在 CentOS 系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了 "ModuleNotFoundError: No module named '_ctypes'" 的错误。

解决

  • yum 安装 libffi-devel
yum install libffi-devel
  • 重装 python,此处省略;
  • 问题解决。

你可能感兴趣的:(Python:pip 安装第三方包时报 ModuleNotFoundError: No module named '_ctypes' 错误)