python2.7安装sqlite3模块

转载自:https://riverdba.github.io/2017/08/29/sqlite3-install-for-python/

摘要:使用sqlmap时遇到一个问题,需要给python安装sqlite3。记录下安装过程!

安装sqlite3

尝试使用pip安装,安装失败:

[root@mysql1 src]# 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-GKbPwN/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-GKbPwN/sqlite3/

下载安装sqlite3

[root@mysql1 src]# wget https://sqlite.org/2017/sqlite-autoconf-3190300.tar.gz
[root@mysql1 src]# tar -xvf sqlite-autoconf-3190300.tar.gz 
[root@mysql1 sqlite-autoconf-3190300]# ./configure --prefix=/usr/local/lib/python2.7/dist-packages/sqlite3
[root@mysql1 sqlite-autoconf-3190300]# make && make install
安装完毕后发现导入sqlite3仍然失败,只能重装python了
>>> 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
>>> exit()

修改python安装源文件setup.py

[root@mysql1 src]# tar -xvf Python-2.7.10.tgz 
[root@mysql1 src]# cd Python-2.7.10    
[root@mysql1 Python-2.7.10]# vi setup.py 
搜索sqlite3找到第1105行,在其下面加入一行上面我们安装的sqlite3的路径:
   1097         # We hunt for #define SQLITE_VERSION "n.n.n"
   1098         # We need to find >= sqlite version 3.0.8
   1099         sqlite_incdir = sqlite_libdir = None
   1100         sqlite_inc_paths = [ '/usr/include',
   1101                              '/usr/include/sqlite',
   1102                              '/usr/include/sqlite3',
   1103                              '/usr/local/include',
   1104                              '/usr/local/include/sqlite',
   1105                              '/usr/local/include/sqlite3',
   1106                              '/usr/local/lib/python2.7/dist-packages/sqlite3',  #新增此行
   1107                            ]
保存退出。

重新编译python2.7

[root@mysql1 Python-2.7.10]# ./configure 
[root@mysql1 Python-2.7.10]# make && make install

验证安装

检查lib文件是否生成

[root@mysql1 Python-2.7.10]# ll /usr/local/lib/python2.7/lib-dynload/_sqlite3.so 
-rwxr-xr-x 1 root root 243444 Aug 30 01:15 /usr/local/lib/python2.7/lib-dynload/_sqlite3.so

验证sqlite3模块导入是否成功

[root@mysql1 Python-2.7.10]# python
Python 2.7.10 (default, Aug 30 2017, 01:14:38) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
[root@mysql1 Python-2.7.10]#

<完>

    原文作者:sqlite
    原文地址: https://www.cnblogs.com/yiruparadise/p/8193698.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞