简体   繁体   中英

Cannot import sqlite3 in Python3

I am unable to import the sqlite3 module in Python, version 3.5.0. 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. 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. I can open up the Python REPL and import sqlite3 just fine when using Python 2.6.6. I can also use sqlite3 from straight from bash and nothing seems awry.

  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. Nope, same error still occurs.

  3. I've been using virtual environments like a good boy, but I have root access to this server. So, I was a bad boy and ran python3 as root without any virtualenvs activated. 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' . 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. ... 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). If you are getting this error, chances are you are using a version of python that was compiled without the correct headers. Here's a step by step guide to get it sorted. (Python 3.XX instructions)

  1. Install the required sqlite libraries
   sudo apt-get install libsqlite3-dev 
  1. Uninstall python (I'm using python 3.6.5 as an example in this guide)
   sudo apt-get remove python3.6
  1. Download python from source
   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. 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:

sudo apt-get install zlib1g-dev

Install sqlite-devel package which includes header, library that is required to build sqlite3 extension.

yum install sqlite-devel

NOTE : Python does not include sqlite3 library itself, but an extension module (wrapper).

I copied sqlite3.dll to this folder:

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

It worked after two weeks trying everything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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