1

Both brew installed python3 and manually compiled python3 with -–enable-loadable-sqlite-extensions fails when import sqlite from python3 shell. Please help!

  • Sorry, ignore the above comment. Error message: Python 3.3.0 (default, Feb 11 2013, 08:22:56) >>> import sqlite3 Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/sqlite3/__init__.py", line 23, in from sqlite3.dbapi2 import * File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/sqlite3/dbapi2.py", line 26, in from _sqlite3 import * ImportError: No module named '_sqlite3' –  Feb 11 '13 at 18:11

2 Answers2

2

The module is named sqlite3, not sqlite:

import sqlite3

http://docs.python.org/3/library/sqlite3.html

Update: Now that we've cleared up the module name, the problem being reported:

ImportError: No module named '_sqlite3'

means that your Python instance cannot find the C extension module, _sqlite3.so, that is part of the sqlite3 module in the standard library. Since the file path of the dbapi2.py in the traceback looks reasonable, the issue is probably not a path issue (sys.path). Most likely the _sqlite3 extension module failed to build or link. Check the output from your Python build for errors. OS X 10.8 includes a version of sqlite3 but for security reasons it does not include the optional loadable extensions feature. Your Python build likely included this message:

Failed to build these modules:
_sqlite3

and, earlier, this:

*** WARNING: renaming "_sqlite3" since importing it failed: dlopen(build/lib.macosx-10.8-x86_64-3.3-pydebug/_sqlite3.so, 2): Symbol not found: _sqlite3_enable_load_extension
  Referenced from: build/lib.macosx-10.8-x86_64-3.3-pydebug/_sqlite3.so
  Expected in: flat namespace
 in build/lib.macosx-10.8-x86_64-3.3-pydebug/_sqlite3.so

The solution is to build and install a separate copy of sqlite3 that is built with the loadable extensions feature. If you are using Homebrew, its sqlite recipe with the with-functions option should do that. Then rebuild Python.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • Thanks for your help Ned. I think brew install sqlite --with-functions completed just fine, but brew install python3 still ended up with same import sqlite3 error. My guess is it did not know about the brew installed version of sqlite, and still linked to the system sqlite. Any idea how to link python3 to brew sqlite? In the end, [a hint from github](https://github.com/mxcl/homebrew/issues/17765) gave me what I wanted, but it doesn't feel like the right way to do it. I wonder if this will lead to conflict with the system sqlite3 later down the line. –  Feb 12 '13 at 01:13
  • I don't follow Homebrew development closely but, in general, the project is quite careful to avoid conflicts with Apple-supplied software. It's not a problem to have more than one copy of the sqlite3 lib installed in different locations. – Ned Deily Feb 12 '13 at 02:38
1

Homebrew provides python3 with sqlite3 support and loadable modules.

brew install python3 will do the right thing (and brew sqlite, too).

There was a bug, that probably struck you, but it has been fixed

Samuel John
  • 1,362
  • 10
  • 12