在Python 3中无法导入sqlite3。

5

在 Python 2.7 中,SQLite 的工作正常,但是当我尝试在 Python 3 中导入它时,会出现错误。

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

为了解决这个错误,我正在尝试重新安装sqlite3:

 sudo apt-get install sqlite3

但是它说该软件包已经存在。之后,我尝试通过

安装它。
pip3 install sqlite3

但是在安装过程中,又出现了错误。

Collecting sqlite3
 Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/sqlite3/
 Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/sqlite3/
 Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after  connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb5ff3bc550>, 'Connection to 196.1.114.80 timed out. (connect timeout=15)')': /simple/sqlite3/
 Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/sqlite3/
 Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/sqlite3/
 Could not find a version that satisfies the requirement sqlite3 (from versions: )
 No matching distribution found for sqlite3

但我的连接工作正常......现在我应该怎么做才能在Python 3中导入sqlite3?

1个回答

6

sqlite3是Python标准库中的一个可选组件。它会在你编译和安装Python 3时一并编译,但前提是正确的sqlite3包含文件(开发头文件)可用。

如果你是自己编译和安装Python 3的,需要安装依赖项(例如libsqlite3-devsqlite-devel等,取决于你使用的Linux发行版),然后重新编译和安装Python 3。

从外部来看,该库作为pysqlite维护;但该版本不支持Python 3。即使如此,要安装它,你仍然需要那些sqlite开发文件,并且需要将其移植到Python 3。干脆重新编译Python 3吧。


我正在使用Ubuntu 16.04,按照某个网站的建议已经安装了libsqlite3-dev,但是仍然无法正常工作... :( - Aman Jaiswal
编译时出现错误:***警告:重命名“_sqlite3”,因为导入失败:build/lib.linux-x86_64-3.6/_sqlite3.cpython-36m-x86_64-linux-gnu.so: undefined symbol: sqlite3_stmt_readonly根据https://github.com/ghaering/pysqlite/issues/85,可能是由于较旧的sqlite版本引起的。 - chehsunliu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接