Ubuntu环境下Python2.7 import sqlite3报错解决方式

系列文章目录


安装Python-2.7

apt-get install build-essential libssl-dev

wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz

# 解压源代码
tar -xJf Python-2.7.18.tar.xz

# 进入解压后的目录
cd Python-2.7.18

# 配置编译选项
./configure

# 编译
make

# 安装
sudo make install

# python2.7 --version
Python 2.7.18

//    安装pip
# wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
# python2.7 get-pip.py 
# pip --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

检查sqlite3是否安装

# python2.7
>>> import sqlite3
-----------
  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

-----

//下载源安装包 安装

# wget https://sqlite.org/2017/sqlite-autoconf-3190300.tar.gz
# tar -xzvf sqlite-autoconf-3190300.tar.gz 
cd sqlite-autoconf-3190300
./configure --prefix=/usr/local/lib/python2.7/dist-packages/sqlite3
make && make install

# python2.7
>>> import sqlite3

…同上还是失败

# cd Python-2.7.18
# vim setup.py /sqlite3搜索找到第1122行,在其下面加入一行上面我们安装的sqlite3的路径:
------------------
        # We need to find >= sqlite version 3.0.8
        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',           // 加上这行
                           ]


-------------保存退出
//重新编译安装python2.7
./configure 
make && make install
//检查lib文件是否生成
# ll /usr/local/lib/python2.7/lib-dynload/_sqlite3.so 

//验证sqlite3模块导入是否成功
# python2.7
>>> import sqlite3
>>>

还是失败。。。— 2.7.14版本有成功的案例:https://blog.csdn.net/xiangliangyu/article/details/82494645

少了一个这,2.7.18编译后没有_sqlite3.so文件

cp ./build/lib.linux-i686-2.7/_sqlite3.so /usr/local/python2.7/lib/python2.7/lib-dynload/

删除源码安装的python2.7.18

# find / -name python2.7
/usr/local/lib/python2.7
/usr/local/include/python2.7
/usr/local/bin/python2.7
/usr/share/bash-completion/completions/python2.7

# rm -rf /usr/local/lib/python2.7
rm -rf /usr/local/include/python2.7
rm -rf /usr/local/bin/python2.7
rm -rf /usr/share/bash-completion/completions/python2.7

//下载安装python2.7.14。。。一样

//直接安装导入还是不行。

# apt install sqlite3
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  sqlite3-doc
The following NEW packages will be installed:
  sqlite3
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 144 kB of archives.
After this operation, 583 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 sqlite3 amd64 3.45.1-1ubuntu2.1 [144 kB]
Fetched 144 kB in 1s (96.8 kB/s)  
Selecting previously unselected package sqlite3.
(Reading database ... 146597 files and directories currently installed.)
Preparing to unpack .../sqlite3_3.45.1-1ubuntu2.1_amd64.deb ...
Unpacking sqlite3 (3.45.1-1ubuntu2.1) ...
Setting up sqlite3 (3.45.1-1ubuntu2.1) ...
Processing triggers for man-db (2.12.0-4build2) ...
Scanning processes...                                                                                                                      
Scanning processor microcode...                                                                                                            
Scanning linux images...                                                                                                                   

Running kernel seems to be up-to-date.

The processor microcode seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.


# python2.7
Python 2.7.14 (default, Apr  8 2025, 00:25:07) 
[GCC 13.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 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

apt 可以安装,但是python2.7中也还是无法导入。。。

# apt-get install sqlite3
# sqlite3 --version
3.45.1 2024-01-30 16:01:20 e876e51a0ed5c5b3126f52e532044363a014bc594cfefa87ffb5b82257ccalt1 (64-bit)

最终解决方式:

apt install -y libsqlite3-dev
./configure --enable-loadable-sqlite-extensions && make && make install

# python2.7
Python 2.7.18 (default, Apr  9 2025, 06:39:19) 
[GCC 13.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> 
不报错了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风云笑gg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值