如何在共享主机上运行sqlite?
我正在尝试用 Python 2.6.4 运行一个脚本。我的托管公司只安装了 2.4 版本,所以我在一个相似的服务器上编译了自己的 2.6.4 版本,然后把文件移动到了 ~/opt/python 目录下。这个步骤看起来没问题。
不过,当我运行下面的脚本时,出现了 ImportError: No module named _sqlite3
的错误,我不知道该怎么解决。
网上大多数讨论都提到 sqlite 或 sqlite3 是包含在 Python 2.6 里的,所以我不明白为什么会出错。
-jailshell-3.2$ ./pyDropboxValues.py Traceback (most recent call last): File "./pyDropboxValues.py", line 21, in import sqlite3 File "/home/myAccount/opt/lib/python2.6/sqlite3/__init__.py", line 24, in from dbapi2 import * File "/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in from _sqlite3 import * ImportError: No module named _sqlite3
我觉得我的目录结构设置得没问题。
-jailshell-3.2$ find `pwd` -type d /home/myAccount/opt /home/myAccount/opt/bin /home/myAccount/opt/include /home/myAccount/opt/include/python2.6 /home/myAccount/opt/lib /home/myAccount/opt/lib/python2.6 /home/myAccount/opt/lib/python2.6/distutils /home/myAccount/opt/lib/python2.6/distutils/command /home/myAccount/opt/lib/python2.6/distutils/tests /home/myAccount/opt/lib/python2.6/compiler /home/myAccount/opt/lib/python2.6/test /home/myAccount/opt/lib/python2.6/test/decimaltestdata /home/myAccount/opt/lib/python2.6/config /home/myAccount/opt/lib/python2.6/json /home/myAccount/opt/lib/python2.6/json/tests /home/myAccount/opt/lib/python2.6/email /home/myAccount/opt/lib/python2.6/email/test /home/myAccount/opt/lib/python2.6/email/test/data /home/myAccount/opt/lib/python2.6/email/mime /home/myAccount/opt/lib/python2.6/lib2to3 /home/myAccount/opt/lib/python2.6/lib2to3/pgen2 /home/myAccount/opt/lib/python2.6/lib2to3/fixes /home/myAccount/opt/lib/python2.6/lib2to3/tests /home/myAccount/opt/lib/python2.6/xml /home/myAccount/opt/lib/python2.6/xml/parsers /home/myAccount/opt/lib/python2.6/xml/sax /home/myAccount/opt/lib/python2.6/xml/etree /home/myAccount/opt/lib/python2.6/xml/dom /home/myAccount/opt/lib/python2.6/site-packages /home/myAccount/opt/lib/python2.6/logging /home/myAccount/opt/lib/python2.6/lib-dynload /home/myAccount/opt/lib/python2.6/sqlite3 /home/myAccount/opt/lib/python2.6/sqlite3/test /home/myAccount/opt/lib/python2.6/encodings /home/myAccount/opt/lib/python2.6/wsgiref /home/myAccount/opt/lib/python2.6/multiprocessing /home/myAccount/opt/lib/python2.6/multiprocessing/dummy /home/myAccount/opt/lib/python2.6/curses /home/myAccount/opt/lib/python2.6/bsddb /home/myAccount/opt/lib/python2.6/bsddb/test /home/myAccount/opt/lib/python2.6/idlelib /home/myAccount/opt/lib/python2.6/idlelib/Icons /home/myAccount/opt/lib/python2.6/tmp /home/myAccount/opt/lib/python2.6/lib-old /home/myAccount/opt/lib/python2.6/lib-tk /home/myAccount/opt/lib/python2.6/hotshot /home/myAccount/opt/lib/python2.6/plat-linux2 /home/myAccount/opt/lib/python2.6/ctypes /home/myAccount/opt/lib/python2.6/ctypes/test /home/myAccount/opt/lib/python2.6/ctypes/macholib /home/myAccount/opt/share /home/myAccount/opt/share/man /home/myAccount/opt/share/man/man1
最后,这是 sqlite3 目录的内容:
-jailshell-3.2$ find `pwd` /home/myAccount/opt/lib/python2.6/sqlite3 /home/myAccount/opt/lib/python2.6/sqlite3/__init__.pyo /home/myAccount/opt/lib/python2.6/sqlite3/dump.pyc /home/myAccount/opt/lib/python2.6/sqlite3/__init__.pyc /home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.pyo /home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.pyc /home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.py /home/myAccount/opt/lib/python2.6/sqlite3/dump.pyo /home/myAccount/opt/lib/python2.6/sqlite3/__init__.py /home/myAccount/opt/lib/python2.6/sqlite3/dump.py
我感觉我需要在 sqlite3
目录里添加一些东西,也许是 sqlite3.so?但我不知道从哪里找这个文件。
我哪里做错了呢?请记住,我是在共享主机上,这意味着我需要在另一台服务器上安装/编译,然后再把文件复制过来。谢谢! :)
更新
我只是想确认一下,@samplebias 的回答确实很有效。我需要在我编译的机器上安装开发包,这样才能添加 sqlite3.so 和相关文件。还发现答案中的链接非常有用。谢谢你,@samplebias!
4 个回答
一般来说,第一步就是问问你的主机提供商。听起来有点奇怪,SQLite居然没有安装(或者没有安装好)。所以如果你问他们,他们很可能会很快帮你解决这个问题。
对于 Python 2.4,你需要安装 sqlite 和一些绑定库,具体来说就是 pysqlite 2 或者 apsw。
Python的构建系统使用一个叫做setup.py的文件来编译所有的本地扩展,包括sqlite3。它会在常见的操作系统路径中查找sqlite3的包含文件和库目录。如果你的系统没有安装sqlite3的开发包,Python就会跳过编译_sqlite3.so
这个扩展,但sqlite3包中的纯Python部分还是会被安装。
在你编译Python和运行时,你需要确保操作系统上安装了sqlite3的开发包:在Centos上是sqlite3-devel
,在Ubuntu上则需要安装libsqlite3-0
和libsqlite3-dev
。
以下是我在Ubuntu系统上_sqlite3.so
扩展链接的一个例子:
% ldd /usr/lib/python2.6/lib-dynload/_sqlite3.so | grep sqlite3
libsqlite3.so.0 => /usr/lib/libsqlite3.so.0 (0x00007f29ef3be000)
% dpkg -S /usr/lib/libsqlite3.so.0
libsqlite3-0: /usr/lib/libsqlite3.so.0