我试图通过import
函数使用sqlite
与python,但看起来python找不到sqlite
。
我的系统。路径包含以下内容:
['', '/usr/local/share/python', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/distribute-0.6.26-py2.7.egg', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/usr/local/Cellar/python/2.7.3/lib/python27.zip', '/usr/local/Cellar/python/2.7.3/lib/python2.7', '/usr/local/Cellar/python/2.7.3/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.3/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.3/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-dynload', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info', '/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/IPython/extensions']
但sqlite
列在usr/lib
中。
编辑:
按照建议,我尝试了import sqlite3
,但是python返回了这个错误:
dlopen(/usr/local/Cellar/python/2.7.3/lib/python2.7/lib-dynload/_sqlite3.so, 2): Library not loaded: /usr/local/lib/libsqlite3.0.8.6.dylib
Referenced from: /usr/local/Cellar/python/2.7.3/lib/python2.7/lib-dynload/_sqlite3.so
Reason: image not found
如何加载sqlite
?
不需要在模块搜索路径中添加任何内容;SQLite模块随Python标准库一起提供。但是,您拼错了模块的名称,它被称为sqlite3
(注意末尾的3
):
Python 2.7.3 (default, Oct 22 2012, 06:12:32)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.connect(':memory:')
<sqlite3.Connection object at 0x105354200>
如果你仍然得到错误,你的自制安装是坏的;你可能遇到了这个bug。运行:
brew rm sqlite python
brew install python
修复。sqlite3是python模块的名称,该模块提供python和sqlite数据库引擎之间的接口。Sqlite是底层数据库引擎的名称。
所以(正如martinjn Pieters已经指出的)试试:
import sqlite3