我会尽力尽可能清晰和具体,因为我见过很多人遇到同样的问题,但他们没有一个答案对我有用。
所讨论的问题是:尝试导入其他模块时缺少模块,(据我所知)Python 3.11 中默认安装了这些模块。 eg:导入sqlite3时缺少_sqlite3。
Python 3.11.0 (main, Jan 12 2023, 03:19:52) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/__init__.py",
line 57, in <module> from sqlite3.dbapi2 import *
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/dbapi2.py",
line 27, in <module> from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
背景
我目前正在使用 Dreamhost VPS,在 Ubuntu 20.04.5 上运行。因为它是 Dreamhost VPS,所以请记住我不能使用 sudo。
我已经全局安装了 Python 3.8,但我正在尝试使用 venv 设置 Python 3.11。安装 Python 3.11 的步骤如“Installing a custom version of Python 3 by Dreamhost”所示:
运行 make 命令时,我看到以下消息:
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_lzma _tkinter _uuid
readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py have not
been built, they are *disabled* by configure:
_sqlite3
这是我的 setup.py 中的 detect_modules():
def detect_modules(self):
# remove dummy extension
self.extensions = []
# Some C extensions are built by entries in Modules/Setup.bootstrap.
# These are extensions are required to bootstrap the interpreter or
# build process.
self.detect_simple_extensions()
self.detect_test_extensions()
self.detect_readline_curses()
self.detect_crypt()
self.detect_openssl_hashlib()
self.detect_hash_builtins()
self.detect_dbm_gdbm()
self.detect_sqlite()
self.detect_platform_specific_exts()
self.detect_nis()
self.detect_compress_exts()
self.detect_expat_elementtree()
self.detect_multibytecodecs()
self.detect_decimal()
self.detect_ctypes()
self.detect_multiprocessing()
self.detect_tkinter()
self.detect_uuid()
# Uncomment the next line if you want to play with xxmodule.c
# self.add(Extension('xx', ['xxmodule.c']))
self.addext(Extension('xxlimited', ['xxlimited.c']))
self.addext(Extension('xxlimited_35', ['xxlimited_35.c']))
运行我刚才提到的命令后,我继续设置 virtualenv 来测试 Python 3.11:
然后我得到了我在问题开头提到的ModuleNotFoundError。
我尝试/看到的事情:
当我检查Python 3.11目录时,我在/home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload中看到我没有的模块的cpython文件只是不在那里,例如:/home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so* 在我的位置Python 3.8,它们确实在那里:/usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so
我尝试将 cpython 文件从 3.8 复制到 3.11 并重命名:
然后,当我运行 Python 3.11 时,出现以下错误:
Python 3.11.0 (main, Jan 12 2023, 04:33:33) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _sqlite3 # this works just fine
>>> import sqlite3 # throws error
356306 segmentation fault (core dumped) python
尝试重新编译 Python 并运行 ./configure --prefix=$HOME/opt/Python-3.11.0 --enable-loadable-sqlite-extensions 但没有成功。
希望我说得足够清楚,并提前致谢。
当ppa中已经有东西可用时,为什么我们需要从源代码编译和安装。从ppa安装python3.11的简单方法如下:
sudo apt update
sudo apt upgrade
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.11
检查版本
python3 --version
甚至
python3.11 --version
来自 https://www.sqlite.org/download.html
下载最新版本的源代码(例如sqlite-autoconf-*.tar.gz)
然后在展开的目录中:
./configure --prefix=/tmp/sqlite3 && make -j4 install
现在在Python源目录中:
export PKG_CONFIG_PATH=/tmp/sqlite3/lib/pkgconfig
./configure --enable-loadable-sqlite-extensions && make -j4
sqlite3 模块应该可以在新建的 Python 中工作