Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import SQLite with Python 2.6

Tags:

python

sqlite

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
>>>

How do I resolve this?

like image 241
David McLaughlin Avatar asked Oct 24 '08 12:10

David McLaughlin


3 Answers

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.

like image 161
razong Avatar answered Oct 01 '22 21:10

razong


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
like image 43
gimel Avatar answered Oct 01 '22 20:10

gimel


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.

like image 44
Dickon Reed Avatar answered Oct 01 '22 22:10

Dickon Reed