python3 pandas模块import报错: 不编译解决 ModuleNotFoundError: No module named '_ctypes'
前言全局说明
ubuntu 18.04
Python 3.8.17
一、环境
ubuntu 18.04
Python 3.8.17
二、报错内容
Traceback (most recent call last):
File "main.py", line 23, in <module>
import pandas as pd
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/__init__.py", line 22, in <module>
from pandas.compat import is_numpy_dev as _is_numpy_dev # pyright: ignore # noqa:F401
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/compat/__init__.py", line 25, in <module>
from pandas.compat.numpy import (
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/compat/numpy/__init__.py", line 4, in <module>
from pandas.util.version import Version
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/util/__init__.py", line 8, in <module>
from pandas.core.util.hashing import ( # noqa:F401
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/core/util/hashing.py", line 24, in <module>
from pandas.core.dtypes.common import (
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/core/dtypes/common.py", line 26, in <module>
from pandas.core.dtypes.base import _registry as registry
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/core/dtypes/base.py", line 24, in <module>
from pandas.errors import AbstractMethodError
File "/home/qtbuild/.local/lib/python3.8/site-packages/pandas/errors/__init__.py", line 6, in <module>
import ctypes
File "/usr/local/python3817/lib/python3.8/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
三、网上的,解决方法
安装依赖库,重新编译安装
centos
sudo yum install libffi-devel -y
Ubuntu
sudo apt install libffi-dev -y
四、解决方法
翻遍了网上文章,都是让执行完上面步骤三,再重新编译安装,但是在生产环境再编译安装,风险比较大,就当我快要放弃,准备重新编译安装时,
看到 https://blog.csdn.net/qq_43189737/article/details/134065576 文章,
里面提到 so 名字,突然灵感一现,报错不就是缺少so文件么,在其他地方找相应的版本,编译好,把其他环境的so放到服务器上so目录里不就行了。经过测试,确实可行。
sudo cp _ctypes.cpython-38-x86_64-linux-gnu.so /usr/local/python3
817/lib/python3.8/lib-dynload/
cpython-38 要注意名字,有的可能是 cpython-38m ,多了个m
另:
之前尝试用系统里3.6版本的so 放到 3.8里,失败了,所以在别的地方编译好,放到生产环境目录里就行。
免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。
参考、来源:
https://blog.csdn.net/ccgshigao/article/details/115315790
https://www.cnblogs.com/qa-freeroad/p/14575832.html (有官方介绍)