简体   繁体   English

无法在 Python3 中导入 sqlite3

[英]Cannot import sqlite3 in Python3

I am unable to import the sqlite3 module in Python, version 3.5.0.我无法在 Python 3.5.0 版中导入 sqlite3 模块。 Here's what I get:这是我得到的:

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

I know, I know, there are PLENTY of StackOverflow posts and support forums across the web where people complain about this problem, but none of the posted solutions have worked for me so far.我知道,我知道,网上有很多 StackOverflow 帖子和支持论坛,人们抱怨这个问题,但到目前为止,没有一个已发布的解决方案对我有用。 Here's where I've been:这是我去过的地方:

  1. I also have Python 2.6.6 installed on this server, which is running CentOS 6.8 x86_64.我还在这台运行 CentOS 6.8 x86_64 的服务器上安装了 Python 2.6.6。 I can open up the Python REPL and import sqlite3 just fine when using Python 2.6.6.在使用 Python 2.6.6 时,我可以打开 Python REPL 并导入 sqlite3 就好了。 I can also use sqlite3 from straight from bash and nothing seems awry.我也可以直接从 bash 中使用 sqlite3 并且没有任何问题。

  2. This helpful question looked promising. 这个有用的问题看起来很有希望。 I tried to re-configure and re-compile Python3.5 with the --enable-loadable-sqlite-extensions option, as user jammyWolf suggested.正如用户 jammyWolf 建议的那样,我尝试使用--enable-loadable-sqlite-extensions选项重新配置和重新编译 Python3.5。 Nope, same error still occurs.不,同样的错误仍然发生。

  3. I've been using virtual environments like a good boy, but I have root access to this server.我一直在使用虚拟环境,就像一个好孩子,但我有这台服务器的 root 访问权限。 So, I was a bad boy and ran python3 as root without any virtualenvs activated.所以,我是一个坏男孩,在没有激活任何虚拟环境的情况下以 root 身份运行 python3。 Still no luck.仍然没有运气。 So I don't think it has anything to do with permissions.所以我认为这与权限无关。

  4. I noticed that in the error message, it says No module named '_sqlite3' .我注意到在错误消息中,它说No module named '_sqlite3' This thread suggests that the underscore before the module name means that the module is an implementation detail, and isn't exposed in the API. 该线程建议模块名称前的下划线表示该模块是一个实现细节,未在 API 中公开。 ... I'm not sure what to make of this information, but there may be a hint somewhere in there. ...我不确定如何处理这些信息,但其中可能有提示。

Any ideas?有什么想法吗?

Falsetru is correct, I am going to go into a bit more detail for those not familiar (linux instructions). Falsetru 是正确的,对于那些不熟悉的人(linux 说明),我将更详细地介绍。 If you are getting this error, chances are you are using a version of python that was compiled without the correct headers.如果您收到此错误,很可能您使用的是在没有正确头文件的情况下编译的 python 版本。 Here's a step by step guide to get it sorted.这是对其进行排序的分步指南。 (Python 3.XX instructions) (Python 3.XX 说明)

  1. Install the required sqlite libraries安装所需的 sqlite 库
   sudo apt-get install libsqlite3-dev 
  1. Uninstall python (I'm using python 3.6.5 as an example in this guide)卸载 python(我在本指南中使用 python 3.6.5 作为示例)
   sudo apt-get remove python3.6
  1. Download python from source从源代码下载python
   cd /tmp && wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
  1. Unpack the archive解压存档
   tar -xvf Python-3.6.5.tgz
  1. Configure配置
   cd Python-3.6.5 && ./configure
  1. Make and install (and go make coffee while you're at it)制作和安装(在你做咖啡的时候去煮咖啡)
   make && sudo make install

If you did everything correctly running "python3.6 -V" should give you your python version.如果你做的一切都正确,运行“python3.6 -V”应该会给你你的python版本。 Note you will have to rebuild any virtual environments you have as well.请注意,您还必须重建您拥有的任何虚拟环境。

One final caveat you may encounter.您可能会遇到的最后一个警告。

zipimport.ZipImportError: can't decompress data; zlib not available

This happens if you don't have the following zlib library installed:如果您没有安装以下 zlib 库,则会发生这种情况:

sudo apt-get install zlib1g-dev

Install sqlite-devel package which includes header, library that is required to build sqlite3 extension.安装sqlite-devel包,其中包括构建sqlite3扩展所需的头文件和库。

yum install sqlite-devel

NOTE : Python does not include sqlite3 library itself, but an extension module (wrapper).注意:Python 不包括sqlite3库本身,而是一个扩展模块(包装器)。

I copied sqlite3.dll to this folder:我将 sqlite3.dll 复制到此文件夹中:

C:\Users\*****\Anaconda3\DLLs

It worked after two weeks trying everything else.经过两周的尝试,它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM