python3.6导入sqlite3错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3.6导入sqlite3错误相关的知识,希望对你有一定的参考价值。

我在ubuntu16.04上安装了Python3.6并安装了sqlite3。在python2中,我可以成功导入sqlite,但是在python3中我遇到了导入错误。我尝试了很多来自Google的方法,但它仍然不起作用。我想知道如何解决它。

Python 3.6.0 (default, Mar 13 2017, 06:38:19) 
[GCC 5.4.0 20160609] 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.6/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.6/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *

ModuleNotFoundError: No module named '_sqlite3'
答案

你可以自己安装python3和sqlite。 try this.

或者您可以尝试如下,

1. install sqlite3
$ wget https://www.sqlite.org/2017/sqlite-autoconf-3170000.tar.gz --no-check-certificate
$ tar zxvf sqlite-autoconf-3170000.tar.gz
$ cd sqlite-autoconf-3170000
$ ./configure --prefix=/usr/local/sqlite3 --disable-static --enable-fts5 --enable-json1 CFLAGS="-g -O2 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS4=1 -DSQLITE_ENABLE_RTREE=1"

2. install python3.6
$ cd Python-3.6.0 
$ LD_RUN_PATH=/usr/local/sqlite3/lib ./configure --prefix=/usr/local/python3.6 LDFLAGS="-L/usr/local/sqlite3/lib" CPPFLAGS="-I /usr/local/sqlite3/include"
$ LD_RUN_PATH=/usr/local/sqlite3/lib make
$ LD_RUN_PATH=/usr/local/sqlite3/lib make install 

另一答案

sqlite3将附带python本身。我也得到了同样的问题,我刚刚卸载了python3.6并再次安装。

卸载现有的python:

sudo apt-get remove --purge python3.6

安装python3.6:

sudo apt install -y 
  build-essential 
  checkinstall

sudo apt install -y 
  libreadline-gplv2-dev 
  libncursesw5-dev 
  libssl-dev 
  libsqlite3-dev 
  tk-dev 
  libgdbm-dev 
  libc6-dev 
  libbz2-dev

PYTHON_VERSION=3.6.0

wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz
tar xvf Python-${PYTHON_VERSION}.tar.xz
cd Python-${PYTHON_VERSION}/
./configure
sudo make altinstall

有用!

以上是关于python3.6导入sqlite3错误的主要内容,如果未能解决你的问题,请参考以下文章

sqlite3:用python创建函数regexp

Python3.6的Enum类型导入失败

Python 3.6.4 dbus 导入错误

Sqlite3 奇怪的行为

python解决No module named _sqlite3的问题

Python3.6 Linux下配置 QQBot 教程