Why my python does not see pysqlite?

2020-02-15 06:34发布

I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem was to install a new version of Python. I do not have the root permissions on the machine. So, I installed it locally. The new version of Python is (2.6.2). As far as I know this version should contain pysqlite by default (and now it is called "sqlite3", not "pysqlite2", as before).

However, if I type:

from sqlite3 import *

I get:

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

It has to be noted, that the above error message is different from those which I get if I type "from blablabla import *":

Traceback (most recent call last):
File "", line 1, in ImportError: No module named blablabla

So, python see something related with pysqlite but still has some problems. Can anybody help me, pleas, with that issue?

P.S. I use CentOS release 5.3 (Final).

2条回答
狗以群分
2楼-- · 2020-02-15 07:17

You have a "slite3.py" (actually its equivalent for a package, sqlite3/__init__.py, so import sqlite3 per se is fine, BUT that module in turns tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. It should be in python2.6/lib-dynload under your local Python root, AND ld should be instructed that it has permission to load dynamic libraries from that directory as well (typically by setting appropriate environment variables e.g. in your .bashrc). Do you have that lib-dynload directory? What's in it? What environment variables do you have which contain the string LD (uppercase), i.e. env|grep LD at your shell prompt?

查看更多
SAY GOODBYE
3楼-- · 2020-02-15 07:23

On Windows, _sqlite3.pyd resides in C:\Python26\DLLs. On *nix, it should be under a path similar to /usr/lib/python2.6/lib-dynload/_sqlite3.so. Chances are that either you are missing that shared library or your PYTHONPATH is set up incorrectly.

Since you said you did not install as a superuser, it's probably a malformed path; you can manually have Python search a path for _sqlite3.so by doing

import sys
sys.path.append("/path/to/my/libs")

but the preferred approach would probably be to change PYTHONPATH in your .bashrc or other login file.

查看更多
登录 后发表回答