19

On Redhat 4.4.7-18 I am trying to run python3 code using sqlite, but I get the following import error:

Traceback (most recent call last):
  File "database.py", line 7, in <module>
    import sqlite3
  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'

I tried to install it:

>sudo pip install sqlite3
Collecting sqlite3
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', error(101, 'Network is unreachable'))': /simple/sqlite3/

(while the network is reachable...) and with the following command:

> sudo yum install sqlite-devel
Loaded plugins: post-transaction-actions, product-id, refresh-packagekit,
              : rhnplugin, search-disabled-repos, security, subscription-manager
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
Package sqlite-devel-3.6.20-1.el6_7.2.x86_64 already installed and latest version
Nothing to do

So it is installed and not installed? Any suggestion how I can solve the original problem?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • 1
    sqlite3 is an optional module *in the standard library*, and it wasn't compiled for your platform. How did you install Python? If you compiled from source you need to have the sqlite development headers available for the module to be compiled. – Martijn Pieters May 16 '17 at 06:28
  • Is there a page describing to check if I have the headers, how to install the headers, how and where to compile sqlite? – Alex May 16 '17 at 06:29
  • on Redhat, I'd look for a libsqlite3-devel package at this point. – Martijn Pieters May 16 '17 at 06:35
  • Please have a look at my original question: I already tried that! It says: "Nothing to do"... – Alex May 16 '17 at 06:36
  • I did ask how you installed Python. The `./configure` script has a `--help` option that details all the different things it'll look for and how to tell it to look elsewhere. If you didn't compile Python on a machine with the SQLite headers in a standard location the `_sqlite3` C module is never compiled, that's all I can tell you, so I gave the standard responses. – Martijn Pieters May 16 '17 at 06:43
  • I do not remember to have installed it. I assume that what is on the computer was there as it was... – Alex May 16 '17 at 06:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144312/discussion-between-alex-and-martijn-pieters). – Alex May 16 '17 at 06:45

4 Answers4

27

Not a direct answer but I ended up here with my search engine... So for my fellow web-surfers:

I had a similar issue, but on ubuntu 16.04 with a manually compile python3.6 version :

    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

I had to install libsqlite3-dev (sudo apt install libsqlite3-dev) and compile from the start python3.6 to make it work.

deterralba
  • 554
  • 6
  • 8
  • 1
    for me I had to use "sudo yum install libsqlite3x-devel.x86_64" for rhel/centos – D Untouchable Apr 24 '20 at 19:55
  • this only for ubuntu and Debian OS – Mr Coder Jul 18 '20 at 16:35
  • 2
    See the linked answer for re-compiling a pyenv env following the instructions in the answer: https://stackoverflow.com/questions/56994638/pyenv-how-to-rebuild-virtualenvs-after-rebuilding-python – svenski Feb 04 '21 at 20:19
  • Does that mean I have to re-compile Python after installing the module? My Python is installed via tools, not download and build on my own. What should I do? – han shih May 16 '22 at 13:38
10

Yep.

sudo yum install sqlite-devel

Followed by rebuild of Python 3.8.3 from source did the trick. Thx!

user2133121
  • 341
  • 1
  • 3
  • 12
6

I had this issue on linux mint 20 after sqlite3 successfully installed

from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

also, could not import sqlite3 into python interpreter

Fix:

sudo apt install libsqlite3-dev

cd your python installer directory

./configure sudo make install

Katja
  • 61
  • 1
  • 1
3

It happens when you install Python using the source code and then compile. Usually people tend to install python and sqlite into separate folders. This causes sqlite binary inaccessible to python executables.

To solve this problem, use a common folder to install BOTH, the Python as well as sqlite. So that python binary and sqlite binary, reside in the same bin folder.

Below example script will solve:

INSTALL_BASE_PATH="/home/<user>/<tools>"
cd ~
mkdir build
cd build
[ -f Python-3.9.10.tgz ] || wget --no-check-certificate https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
tar -zxvf Python-3.9.10.tgz

[ -f sqlite-autoconf-3380000.tar.gz ] || wget --no-check-certificate https://www.sqlite.org/2022/sqlite-autoconf-3380000.tar.gz
tar -zxvf sqlite-autoconf-3380000.tar.gz

cd sqlite-autoconf-3380000
./configure --prefix=${INSTALL_BASE_PATH}
make
make install

cd ../Python-3.9.10
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib configure
LDFLAGS="-L ${INSTALL_BASE_PATH}/lib"
CPPFLAGS="-I ${INSTALL_BASE_PATH}/include"
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib make
./configure --prefix=${INSTALL_BASE_PATH}
make
make install

cd ~
LINE_TO_ADD="export PATH=${INSTALL_BASE_PATH}/bin:\$PATH"
if grep -q -v "${LINE_TO_ADD}" $HOME/.bashrc; then echo "${LINE_TO_ADD}" >> $HOME/.bashrc; fi

LINE_TO_ADD="export LD_LIBRARY_PATH=${INSTALL_BASE_PATH}/lib"
if grep -q -v "${LINE_TO_ADD}" $HOME/.bashrc; then echo "${LINE_TO_ADD}" >> $HOME/.bashrc; fi

source $HOME/.bashrc

Shashi Ranjan
  • 353
  • 2
  • 8