python2.7安裝sqlite3模塊

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

摘要:使用sqlmap時遇到一個問題,須要給python安裝sqlite3。記錄下安裝過程!mysql

安裝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]#

<完>linux

相關文章
相關標籤/搜索