Linux

ModuleNotFoundError:沒有名為“_sqlite3”的模組

  • September 27, 2020

我們安裝並專門使用了不同的 python 版本,python3.7所以我編輯了我的.bashrc文件。我們正在使用Centos7伺服器Linux

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
       . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
alias python=python3.7
alias pip=pip3.7


[xyz@innolx20122 ~]$ python
python             python2.7          python3.6          python3.7          python3.7m-config
python2            python3            python3.6m         python3.7m

[xyz@innolx20122 ~]$ which sqlite3
/usr/bin/sqlite3

它的使用python2.7python3.6版本

[xyz@innolx20122 ~]$ python2.7
Python 2.7.5 (default, Apr  2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

[xyz@innolx20122 ~]$ python3.6
Python 3.6.8 (default, Apr  2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

它不適用於python3.7版本

[xyz@innolx20122 ~]$ python3.7
Python 3.7.0 (default, Sep  3 2020, 09:25:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/lib/python3.7/sqlite3/__init__.py", line 23, in <module>
   from sqlite3.dbapi2 import *
 File "/usr/local/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
   from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

更新-

我們按照以下連結中的說明安裝了python Python3.7安裝連結

因此我的python3.7版本安裝在根級別

[root@innolx20122 ~]# ls
anaconda-ks.cfg  Python-3.7.0  Python-3.7.0.tgz
[root@innolx20122 ~]# cd Python-3.7.0
[root@innolx20122 Python-3.7.0]# ls
aclocal.m4    config.status  Doc         Lib              Mac              Misc     PC              pyconfig.h     python-config     setup.py
build         config.sub     Grammar     libpython3.7m.a  Makefile         Modules  PCbuild         pyconfig.h.in  python-config.py  Tools
config.guess  configure      Include     LICENSE          Makefile.pre     Objects  Programs        python         python-gdb.py
config.log    configure.ac   install-sh  m4               Makefile.pre.in  Parser   pybuilddir.txt  Python         README.rst

我看到一個關於堆棧溢出的連結建議一些解決方法。 修復 Sqlite3 問題

請建議是否可以從同一根目錄本身執行以下命令

yum install sqlite-devel

./configure
make && make altinstall

我以root使用者身份登錄。

[root@innolx20122 ~]# ls
anaconda-ks.cfg  Python-3.7.0  Python-3.7.0.tgz

然後導航到Python-3.7.0目錄

[root@innolx20122 Python-3.7.0]# ls
aclocal.m4    config.status  Doc         Lib              Mac              Misc     PC              pyconfig.h     python-config     setup.py
build         config.sub     Grammar     libpython3.7m.a  Makefile         Modules  PCbuild         pyconfig.h.in  python-config.py  Tools
config.guess  configure      Include     LICENSE          Makefile.pre     Objects  Programs        python         python-gdb.py
config.log    configure.ac   install-sh  m4               Makefile.pre.in  Parser   pybuilddir.txt  Python         README.rst

此後執行以下命令來解決問題。

yum install sqlite-devel

./configure
make && make altinstall


[xyz@innolx20122 ~]$ python3.7
Python 3.7.0 (default, Sep 26 2020, 23:18:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>

引用自:https://unix.stackexchange.com/questions/610875