views:

4451

answers:

10

I'm running Python 2.6 on Unix and when I run the interactive prompt (SQLite is supposed to be preinstalled) I get:

[root@idev htdocs]# python
Python 2.6 (r26:66714, Oct 23 2008, 16:25:34)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>> import sqlite
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>>

How do I resolve this?

+8  A: 
import sqlite3

sqlite3 - DB-API 2.0 interface for SQLite databases.

You are missing the .so (shared object) - probably an installation step. In my Linux python installation, _sqlite3 is at:

${somewhere}/lib/python2.6/lib-dynload/_sqlite3.so
gimel
A: 

Still getting an error, but different this time:

>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python-2.6/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
    from dbapi2 import *
  File "/usr/local/python-2.6/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: No module named _sqlite3
David McLaughlin
See my edited answer regarding _sqlite3.so
gimel
A: 

Try Python 2.5.

Kirill Titov
+6  A: 

The error:

ImportError: No module named _sqlite3

means that SQLite 3 does not find the associated shared library. On Mac OS X it's _sqlite3.so and it should be the same on other Unix systems.

To resolve the error you have to locate the _sqlite3.so library on your computer and then check your PYTHONPATH for this directory location.

To print the Python search path enter the following in the Python shell:

import sys
print sys.path

If the directory containing your library is missing you can try adding it interactively with

sys.path.append('/your/dir/here')

and try

import sqlite3

again. If this works you have to add this directory permanently to your PYTHONPATH environment variable.

PS: If the library is missing you should (re-)install the module.

razong
A: 

Try this:

from pysqlite2 import dbapi2 as sqlite
André
+1  A: 

On my system _sqlite3.so located at:

'/usr/lib/python2.6/lib-dynload/_sqlite3.so'

Check that the directory is in your sys.path:

>>> import sys; print(filter(lambda p: 'lib-dynload' in p, sys.path))
['/usr/lib/python2.6/lib-dynload']
J.F. Sebastian
+2  A: 

I'm a little confused; the question has import sqlite, but the module is actually named sqlite, so this should be import sqlite3; is the posted interactive interpreter session incorrect?

mithrandi
+5  A: 

Python 2.6 detects where the sqlite3 development headers are installed, and will silently skip building _sqlite3 if it is not available. If you are building from source, install sqlite3 including development headers. In my case, sudo yum install sqlite-devel sorted this out on a CentOS 4.7. Then, rebuild Python from source code.

Dickon Reed
A: 

Does that fix your problem?

Python 2.5.4 (r254:67916, May 31 2009, 16:56:01)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>> import sqlite3
>>>
ΤΖΩΤΖΙΟΥ
A: 

The 2.5.5. Mac port of Python 2.5 now has this hint:

"py25-sqlite3 @2.5.4 (python, databases)
    This is a stub. sqlite3 is now built with python25"

And so an upgrade of the python25 port to python25 @2.5.5_0 made the import work again. Since sqlite3 is among the dependencies of python25, it is built anew when upgrading python25. Thus,

$ sudo port upgrade python25

does the trick on Mac OS X, ports collection.

98