首页 > 代码库 > python安装sqlite3模块

python安装sqlite3模块

Python安装sqlite3 

环境为Ubuntu16.04 Apache2.4 Python2.7.13 django 1.8

今天部署apache+django,经过各种折腾,好不容易配置完了,发现错误Apache的日志里有一项

ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3

于是打开Python 测试下能否导入sqlite3模块

>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
    from dbapi2 import *
  File "/usr/local/lib/python2.7/sqlite3/dbapi2.py", line 28, in <module>
    from _sqlite3 import *
ImportError: No module named _sqlite3

结果是没有,我原来以为Python自带的呢,

用pip install sqlite3 结果安装不了!如下:

Collecting sqlite3
  Using cached sqlite3-99.0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-Tpr4M6/sqlite3/setup.py", line 2, in <module>
        raise RuntimeError("Package ‘sqlite3‘ must not be downloaded from pypi")
    RuntimeError: Package ‘sqlite3‘ must not be downloaded from pypi
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Tpr4M6/sqlite3/

网上参考了两个博客

http://www.pythonclub.org/python-files/sqlite 和

http://www.python88.com/topic/420/

下载与安装SQLITE3

下载 sqlite-autoconf-3190300.tar.gz

在 https://sqlite.org/download.html 这里下载。我下载的是

安装方法是:

  1. 解压
  2. ./configure --prefix=/usr/local/lib/python2.7/dist-packages/sqlite3   #目录下面要用的
  3. make
  4. make install
  5. 安装完测试一下,看是否能导入sqlite3模块,我还是不行

那就重新安装python吧

不管有没有安装过Python,这里都必须重新进行安装,安装前,需要更改源码目录下的setup.py

可以查找sqlite关键字,找到如下行,在末尾加上sqlite的安装路径

sqlite_incdir = sqlite_libdir = None
        sqlite_inc_paths = [ ‘/usr/include‘,
                             ‘/usr/include/sqlite‘,
                             ‘/usr/include/sqlite3‘,
                             ‘/usr/local/include‘,
                             ‘/usr/local/include/sqlite‘,
                             ‘/usr/local/include/sqlite3‘,
                             ‘/usr/local/lib/python2.7/dist-packages/sqlite3‘,
                           ]

安装完后,再测试一下,看是否成功

1. 方法一:先看下Lib是否已经生成

还是在 /home/user/usr/python/python2.7/lib/python2.7/lib-dynload 下

发现了 _sqlite3.so 文件

2. 方法二:使用python 命令

>>> import sqlite3
不报错即可!

python安装sqlite3模块