CentOS 上 python3.7 中的 sqlite3 模块

CentOS 上 python3.7 中的 sqlite3 模块

我正在尝试在 CentOS 上获取 Python 3.7 的可运行版本,但症结似乎在于内置sqlite3模块。

我正在按如下方式构建 Python(为了可重复性,这里使用 Dockerfile):

FROM centos:7.6.1810
RUN yum update -y
RUN yum install -y epel-release
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm

RUN yum install -y python34u python34u-pip
RUN yum install -y python35u python35u-pip
RUN yum install -y python36u python36u-pip

# Python3.7 is not currently available from RHEL, EPEL, or IUS repos so download and compile it
RUN yum install -y gcc openssl-devel bzip2-devel libffi-devel make
ARG PY37_VERSION=3.7.6
RUN cd /usr/src && curl https://www.python.org/ftp/python/${PY37_VERSION}/Python-${PY37_VERSION}.tgz | tar -xz &&\
  cd Python-${PY37_VERSION} && LD_RUN_PATH=/usr/lib ./configure --enable-optimizations && make -j4 altinstall &&\
  rm -rf /usr/src/Python-${PY37_VERSION}
RUN cd /tmp && curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3.7 get-pip.py

当我启动容器并尝试导入模块时sqlite3,出现库错误:

[root@ccea19dd0dc6 /]# python3.7 -c 'import sqlite3'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.7/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

有解决这个问题的已知方法吗?

答案1

我仔细查看了一下setup.pypython 发行版的文件,发现它似乎会检查/usr/includesqlite 文件。我sqlite-devel在构建 python 之前安装了它,看起来它确实有效:

RUN yum install -y gcc openssl-devel bzip2-devel libffi-devel make sqlite-devel
ARG PY37_VERSION=3.7.6
RUN cd /usr/src && curl https://www.python.org/ftp/python/${PY37_VERSION}/Python-${PY37_VERSION}.tgz | tar -xz &&\
  cd Python-${PY37_VERSION} && ./configure --enable-optimizations && make -j4 altinstall &&\
  rm -rf /usr/src/Python-${PY37_VERSION}

现在sqlite3可以加载:

[root@b2ae4f2bc5af Python-3.7.6]# python3.7 -c "import sqlite3"
[root@b2ae4f2bc5af Python-3.7.6]#

相关内容