ModuleNotFoundError: No module named '_sqlite3'

python經常在某些環境安裝的時候會出現,這個錯誤:

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

可能的原因是因為安裝python的時候沒有找到sqlite3.so的庫。
簡單的解決方式:

# 安裝sqlite相關的全部,可能并不需要全部,為了簡單直接用*號代替了
yum install sqlite*
然后重新編譯python或者重新安裝python

如果這種方式解決不了,建議通過編譯安裝sqlite3,下載地址

./configure --prefix=/usr/local/sqlite3
make && make install

然后打開python安裝源碼的setup.py修改如下:

sqlite_inc_paths = [ '/usr/include',
                             '/usr/local/sqlite3/include', #增加該部分內容
                             '/usr/include/sqlite',
                             '/usr/include/sqlite3',
                             '/usr/local/include',
                             '/usr/local/include/sqlite',

重新編譯安裝。
上面的方法我自己在centos6.5、python3.6的環境依舊沒解決問題。最后我是這樣解決的:

#手動安裝sqlite3 完成的時候會有這么一段提示:
----------------------------------------------------------------------
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.
----------------------------------------------------------------------

這段內容顯示了sqlite3的安裝路徑:/usr/local/lib
特別注意add LIBDIR to the 'LD_LIBRARY_PATH' environment variablesqlite建議添加環境變量。

export LD_LIBRARY_PATH=/usr/local/lib

設置一個環境變量,然后重新編譯python3安裝就能夠成功了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容