python3 install 其他包 报:ModuleNotFoundError: No module named '_ctypes' 的问题

python3 install 其他包 报:ModuleNotFoundError: No module named '_ctypes' 的问题

python2
import _ctypes

没有问题

python3
import _ctypes

报上述错误,说明_ctypes确实无法导入。

据网上资料显示时没有安装 libdiff 库,但使用yum安装后,还需要重新编译python3。但python3好像是通过rpm安装进来的,不知道源码在哪里,不会重新编译。

打算使用pyenv安装个3.9的版本来曲线救国。

先安装pyenv

curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

想要全局直接直接使用命令pyenv -v,则需要配置环境:

# 在~/.bash_profile中添加:
export PATH="/root/.pyenv/bin:$PATH" # 注意这里要写自己的目录
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)

# 使配置生效
source ~/.bash_profile

安装3.7.9

pyenv install 3.7.9

报了一堆错,但安装成功了,先不管它(我的python环境仍然还有问题)。

patching file configure.ac
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/root/.pyenv/versions/3.7.9/lib/python3.7/bz2.py", line 19, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/root/.pyenv/versions/3.7.9/lib/python3.7/curses/__init__.py", line 13, in <module>
    from _curses import *
ModuleNotFoundError: No module named '_curses'
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/root/.pyenv/versions/3.7.9/lib/python3.7/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/root/.pyenv/versions/3.7.9/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/root/.pyenv/versions/3.7.9/lib/python3.7/lzma.py", line 27, in <module>
    from _lzma import *
ModuleNotFoundError: No module named '_lzma'
WARNING: The Python lzma extension was not compiled. Missing the lzma lib?
Installed Python-3.7.9 to /root/.pyenv/versions/3.7.9

使用3.7.9

> mkdir py3.7
> cd py3.7
> pyenv local 3.7.9
> python -V 
# 输出: Python 3.7.9 (表示已经OK)
# 测试下_ctypes有没有问题
> python
> import _ctypes
# 没有报错,可以使用

以上解决了_ctypes的问题,当然为了保险起见还是把python的环境给恢复了(这里如果要去回复python的话,之前搞好的yum也要重新弄,嫌麻烦先不管了,以后直接重做系统就好)

posted @ 2023-08-02 12:49  流失的痕迹  阅读(1861)  评论(0编辑  收藏  举报