解决Python3 No module named '_sqlite3'错误

解决Python3 No module named '_sqlite3'错误

 

升级完python3.10.9以及新安装home assistant core 2023.2.2后运行 hass -v项目的时候出现 No module named '_sqlite3' 错误:

2023-02-06 06:20:39.968 ERROR (MainThread) [homeassistant.setup] Setup failed for analytics: Unable to import component: No module named '_sqlite3'

出现此问题: 我们进入python3环境

root@raspberrypi:~# python3
Python 3.10.9 (main, Jan 18 2023, 18:50:28) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/site-packages']
>>> 

完成退出 python环境


执行以下命令:
[root@localhost lib-dynload]# find / -name _sqlite3.so

root@raspberrypi:~# find / -name _sqlite*.so
/snap/core/14402/usr/lib/python3.5/lib-dynload/_sqlite3.cpython-35m-arm-linux-gnueabihf.so
/snap/core/14449/usr/lib/python3.5/lib-dynload/_sqlite3.cpython-35m-arm-linux-gnueabihf.so
/snap/core18/2683/usr/lib/python3.6/lib-dynload/_sqlite3.cpython-36m-arm-linux-gnueabihf.so
/snap/core18/2670/usr/lib/python3.6/lib-dynload/_sqlite3.cpython-36m-arm-linux-gnueabihf.so
find: ‘/proc/8481’: 没有那个文件或目录
/usr/lib/pypy/lib_pypy/_sqlite3_cffi.pypy-73-arm-linux-gnueabihf.so
/usr/lib/python2.7/lib-dynload/_sqlite3.arm-linux-gnueabihf.so
/usr/lib/python3.9/lib-dynload/_sqlite3.cpython-39-arm-linux-gnueabihf.so


发现在/usr/local/python3/lib/python3.10/lib-dynload/这个路径下没找到_sqlite3.so
但是在/usr/lib64/python2.7/lib-dynload/_sqlite3.so是有这个的; 记住千万不要把python2.7下的_sqlite3.so复制到python3下;是用不了的;

接下来我们开始安装sqlite3: 

下载安装包

wget https://sqlite.org/2021/sqlite-autoconf-3360000.tar.gz


解压

tar -xvf sqlite-autoconf-3360000.tar.gz

进入目录

cd sqlite-autoconf-3360000


编译

./configure --prefix=/usr/local/sqlite


安装 

make -j4&&sudo make install


安装成功后如下

root@raspberrypi:~/sqlite-autoconf-3360000# make -j4&&sudo make install
make: 对“all”无需做任何事。
make[1]: 进入目录“/root/sqlite-autoconf-3360000”
 /bin/mkdir -p '/usr/local/sqlite/lib'
 /bin/bash ./libtool   --mode=install /usr/bin/install -c   libsqlite3.la '/usr/local/sqlite/lib'
libtool: install: /usr/bin/install -c .libs/libsqlite3.so.0.8.6 /usr/local/sqlite/lib/libsqlite3.so.0.8.6
libtool: install: (cd /usr/local/sqlite/lib && { ln -s -f libsqlite3.so.0.8.6 libsqlite3.so.0 || { rm -f libsqlite3.so.0 && ln -s libsqlite3.so.0.8.6 libsqlite3.so.0; }; })
libtool: install: (cd /usr/local/sqlite/lib && { ln -s -f libsqlite3.so.0.8.6 libsqlite3.so || { rm -f libsqlite3.so && ln -s libsqlite3.so.0.8.6 libsqlite3.so; }; })
libtool: install: /usr/bin/install -c .libs/libsqlite3.lai /usr/local/sqlite/lib/libsqlite3.la
libtool: install: /usr/bin/install -c .libs/libsqlite3.a /usr/local/sqlite/lib/libsqlite3.a
libtool: install: chmod 644 /usr/local/sqlite/lib/libsqlite3.a
libtool: install: ranlib /usr/local/sqlite/lib/libsqlite3.a
libtool: warning: remember to run 'libtool --finish /usr/local/lib'
 /bin/mkdir -p '/usr/local/sqlite/bin'
  /bin/bash ./libtool   --mode=install /usr/bin/install -c sqlite3 '/usr/local/sqlite/bin'
libtool: install: /usr/bin/install -c sqlite3 /usr/local/sqlite/bin/sqlite3
 /bin/mkdir -p '/usr/local/sqlite/include'
 /usr/bin/install -c -m 644 sqlite3.h sqlite3ext.h '/usr/local/sqlite/include'
 /bin/mkdir -p '/usr/local/sqlite/share/man/man1'
 /usr/bin/install -c -m 644 sqlite3.1 '/usr/local/sqlite/share/man/man1'
 /bin/mkdir -p '/usr/local/sqlite/lib/pkgconfig'
 /usr/bin/install -c -m 644 sqlite3.pc '/usr/local/sqlite/lib/pkgconfig'
make[1]: 离开目录“/root/sqlite-autoconf-3360000”

 

# which sqlite3
/usr/local/bin/sqlite3

 

执行命令import sqlite3 不正常

root@raspberrypi:~# python3
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.10/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
>>> 

 

root@raspberrypi:~/sqlite-autoconf-3360000# ./libtool --finish /usr/local/lib
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

此时我们进行重新安装python3
进入python3安装目录 :
root@raspberrypi:# cd Python-3.10.9

修改setup.py
root@raspberrypi:~/Python-3.10.9# vi setup.py
查找" sqlite_inc_paths" 新增

'/usr/local/sqlite/include'
'/usr/local/sqlite/include/sqlite3'


完成后如下:

       sqlite_incdir = sqlite_libdir = None
        sqlite_inc_paths = [ '/usr/include',
                             '/usr/include/sqlite',
                             '/usr/include/sqlite3',
                             '/usr/local/include',
                             '/usr/local/include/sqlite',
                             '/usr/local/include/sqlite3',
                             '/usr/local/sqlite/include',
                             '/usr/local/sqlite/include/sqlite3',
                             ]

保存退出;

执行命令:

root@raspberrypi:~/Python-3.10.9#  ./configure --enable-loadable-sqlite-extensions
root@raspberrypi:~/Python-3.10.9# make -j 4
root@raspberrypi:~/Python-3.10.9# sudo make install

output

If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations



安装完执行以下命令查看_sqlite3.so情况

root@raspberrypi:~# find / -name _sqlite*.so
/snap/core/14402/usr/lib/python3.5/lib-dynload/_sqlite3.cpython-35m-arm-linux-gnueabihf.so
/snap/core/14449/usr/lib/python3.5/lib-dynload/_sqlite3.cpython-35m-arm-linux-gnueabihf.so
/snap/core18/2683/usr/lib/python3.6/lib-dynload/_sqlite3.cpython-36m-arm-linux-gnueabihf.so
/snap/core18/2670/usr/lib/python3.6/lib-dynload/_sqlite3.cpython-36m-arm-linux-gnueabihf.so
/root/Python-3.10.9/build/lib.linux-armv7l-3.10/_sqlite3.cpython-310-arm-linux-gnueabihf.so
/usr/lib/pypy/lib_pypy/_sqlite3_cffi.pypy-73-arm-linux-gnueabihf.so
/usr/lib/python2.7/lib-dynload/_sqlite3.arm-linux-gnueabihf.so
/usr/lib/python3.9/lib-dynload/_sqlite3.cpython-39-arm-linux-gnueabihf.so
/usr/local/lib/python3.10/lib-dynload/_sqlite3.cpython-310-arm-linux-gnueabihf.so

 

root@raspberrypi:~# python3
Python 3.10.9 (main, Jan 18 2023, 18:50:28) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/site-packages']
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.10/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.10/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
>>> import sqlite3

 

 

 

 

 

https://www.jianshu.com/p/dd4532457b9f

留下你的评论
*
只有注册用户才能评论。