167

Can someone tell me how to install the sqlite3 module alongside the most recent version of Python? I am using a Macbook, and on the command line, I tried:

pip install sqlite

but an error pops up.

Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
Jin-Dominique
  • 3,043
  • 6
  • 19
  • 28
  • 4
    If your python is built from source manually , and meet this error, you should install sqlite-devel package first, then rebuild python, as @falsetru said, the package name will be vary depending on the Operating system. – ngn999 Mar 28 '16 at 01:54
  • For everone trying to build python from source and running into this error: This really good answer adresses the bulild process and the dependencys you need. https://stackoverflow.com/a/6171511/6273503 – Harper Sep 28 '17 at 08:35

6 Answers6

310

You don't need to install sqlite3 module. It is included in the standard library (since Python 2.5).

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 6
    I actually have a python 2.5.4 that does not included sqlite3 :( – shevy Jul 04 '14 at 13:13
  • 1
    @user722915, According to [What's New in Python 2.5](https://docs.python.org/2.7/whatsnew/2.5.html#the-sqlite3-package), The pysqlite module (http://www.pysqlite.org), a wrapper for the SQLite embedded database, has been added to the standard library under the package name sqlite3. – falsetru Jul 04 '14 at 13:16
  • 22
    if your python3 is built from source manually , and meet this error, you should install sqlite-devel package first, then rebuild python3. – ngn999 Mar 28 '16 at 01:28
  • @ngn999, You'd better comment to the question to let OP know it. Because this was answered very long time, and it is not notified to OP if you comment to the answer. – falsetru Mar 28 '16 at 01:49
  • 7
    @ngn999, BTW, the package name will be vary depending on the Operating system. For example, in Ubuntu, it's `libsqlite3-dev`. – falsetru Mar 28 '16 at 01:50
  • 4
    LITERALLY LIFED MY SAVE! – NoName Dec 08 '20 at 02:59
  • @ngn999 your comment should be the answer for python3 users. It solved my problem. – pankaj Feb 09 '21 at 12:45
70

For Python version 3:

pip install pysqlite3 
ted
  • 13,596
  • 9
  • 65
  • 107
Hoss
  • 901
  • 6
  • 3
  • 4
    pysqlite3 is just a wrapper. It won't work if you don't actually have the SQLite libraries in your system (or the devel package if you built Python from sources). – Paulo Carvalho Sep 04 '21 at 20:44
  • 1
    I tried this and it installed, but I still couldn't use it. Any ideas? Here's what it says when I run the command: Preparing metadata (setup.py) ... done Using legacy 'setup.py install' for pysqlite3, since package 'wheel' is not installed. Installing collected packages: pysqlite3 Running setup.py install for pysqlite3 ... done Successfully installed pysqlite3 – ptn77 Mar 28 '22 at 14:19
42

I have python 2.7.3 and this solved my problem:

pip install pysqlite
nicolimo86
  • 718
  • 11
  • 22
26

Normally, it is included. However, as @ngn999 said, if your python has been built from source manually, you'll have to add it.

Here is an example of a script that will setup an encapsulated version (virtual environment) of Python3 in your user directory with an encapsulated version of sqlite3.

INSTALL_BASE_PATH="$HOME/local"
cd ~
mkdir build
cd build
[ -f Python-3.6.2.tgz ] || wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar -zxvf Python-3.6.2.tgz

[ -f sqlite-autoconf-3240000.tar.gz ] || wget https://www.sqlite.org/2018/sqlite-autoconf-3240000.tar.gz
tar -zxvf sqlite-autoconf-3240000.tar.gz

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

cd ../Python-3.6.2
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/.bash_profile; then echo "${LINE_TO_ADD}" >> $HOME/.bash_profile; fi
source $HOME/.bash_profile

Why do this? You might want a modular python environment that you can completely destroy and rebuild without affecting your managed package installation. This would give you an independent development environment. In this case, the solution is to install sqlite3 modularly too.

Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
  • 1
    Can also happen if you use pyenv to manage your Python versions, but you didn't have all the system packages installed when the Python interpreter was installed. – MarkNS Oct 10 '19 at 07:18
  • I had a requirement where I didn't want to disturb the existing python setup. This is exactly what I needed. Thanks !! – Anurag May 06 '21 at 10:39
4

if you have error in Sqlite built in python you can use Conda to solve this conflict

conda install sqlite
Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23
0

For Windows + Conda users: you have to download the sqlite3 dll, uncompress and copy the file into the DLL dir in Conda installion path

Check this answer for more detail

José
  • 1,774
  • 1
  • 17
  • 21