4

我已经安装了 Python 2.6.2。我是“本地”安装的,因为我没有 root 权限。在这个版本的 Python 中,我想使用名为“sqlite3”的模块(在早期版本中它被称为“pysqlite”)。从理论上讲,我必须能够毫无问题地使用这个模块,因为它应该默认包含在所考虑的 Python 版本中。但是,我有一些麻烦。当我输入:

from sqlite3 import *

我得到:

Traceback (most recent call last):
  File "", line 1, in File "/home/verrtex/opt/lib/python2.6/sqlite3/init.py", line 24, in
    from dbapi2 import *
  File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in
    from _sqlite3 import * 
ImportError: No module named _sqlite3

正如我已经告诉过的,这个问题的可能原因是模块尝试导入_sqlite3并失败,所以它没有找到_sqlite3.so。我的“/home/verrtex/opt/lib/python2.6/lib-dynload”目录中没有 _sqlite3.so 文件这一事实支持了这种解释。所以,这是我必须解决的问题(我必须把这个文件放到这个目录中)。

我发现要解决这个问题,我必须“安装 sqlite3 并重新编译 Python”。我还发现可以通过“从源代码构建并将库移动到/usr/lib/python2.5/lib-dynload/”来解决问题。

但是,我不清楚我到底应该做什么。我应该安装名为“sqlite3”的python模块还是应该安装sqlite-database?顺便说一句,我已经由管理员全局安装了 sqlite-database。我可以使用它还是我仍然需要安装我自己的数据库?顺便说一句,我没有root权限。会不会有问题?或者我需要安装一个python模块?在这种情况下,没有 root 权限是一个问题吗?

我还被告知要从 SQLite 下载页面获取源文件,提取存档,移动到扩展目录并执行:

./configure
make
make install

然后我必须将新编译的文件复制到我的 Python 目录中。我应该复制所有新编译的文件吗?我应该复制到哪个目录(我的 Python 目录有一些子目录)?

将不胜感激任何帮助,因为我与这个问题堆叠了一段时间。

PS 我的操作系统是 CentOS release 5.3 (Final)。

4

2 回答 2

1

Although you might have found your solution, I just wrote mine down for someone who are stuck in the same problem.

My OS is CentOS 6.3(Final) with python2.6.

I install python2.7.3 in my system, but the problem's still there. (_sqlite3.so should be in /path/to/python2.7.3/lib/python2.7/lib-dynload after python2.7.3 has been installed. Because before python2.7 was installed, sqlite-autoconf-3071502.tar.gz was installed.)

I then copy the /path/to/python2.6/lib/python2.6/lib-dynload/_sqlite3.so to the python2.7's path. And type in the python-shell:

>>> import sqlite3

or

>>> import _sqlite3

No error reports.

Unfortunately, the damn error appeared as before when I run my python script. I install sqlite-devel(sudo yum install sqlite-devel or download here), and then reinstall python2.7.3 again. Run my python script again. Thank goodness! The damn error finally solved.

于 2013-01-27T08:23:07.030 回答
1

sys.path可能没有指向本地安装的副本,或者您没有运行您认为的 Python 2.6.2。

如果不是这种情况,您需要 SQLite 开发头文件(sqlite-dev 或其他),然后重新编译 Python。您需要在编译结束时注意,因为它会抱怨由于缺少依赖项而未构建的内容。

编辑:重读问题。

编辑2:另外,不要这样做:

from module import *

做这个:

from module import what_i_need
import module2
于 2009-09-25T23:58:45.567 回答