You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS CodeBuild中SQLite已升级但Django仍检测旧版本问题

解决CodeBuild中SQLite升级后Django仍识别旧版本的问题

问题核心:你虽然升级了sqlite3命令行工具,但Python的内置sqlite3模块是编译时绑定系统旧版SQLite库的,进程启动时默认加载系统库,不会自动使用新安装的版本。以下是可行的解决步骤:

方法一:让Python加载新版SQLite动态库

  1. 修改SQLite编译安装脚本
    确保get_sqlite.sh将SQLite安装到独立目录,并更新系统库缓存:

    #!/bin/bash
    set -e
    cd /tmp
    wget https://www.sqlite.org/2022/sqlite-autoconf-3400100.tar.gz
    tar xzf sqlite-autoconf-3400100.tar.gz
    cd sqlite-autoconf-3400100
    ./configure --prefix=/usr/local/sqlite3
    make -j$(nproc)
    sudo make install
    sudo ldconfig /usr/local/sqlite3/lib
    
  2. 在buildspec.yaml中设置环境变量
    在执行Python命令前,指定LD_LIBRARY_PATH让Python加载新的SQLite库,同时更新PATH确保命令行工具优先级:

    post_build:
      commands:
        - echo "beginning post-build phase..."
        - bash get_sqlite.sh
        - export LD_LIBRARY_PATH=/usr/local/sqlite3/lib:$LD_LIBRARY_PATH
        - export PATH=/usr/local/sqlite3/bin:$PATH
        - echo "sqlite version -- $(sqlite3 --version)"
        - python manage.py makemigrations
        ...
    

方法二:用pysqlite3-binary替代内置sqlite3模块

如果方法一无效,可以直接使用预编译的pysqlite3包,它静态链接了新版SQLite,不依赖系统库:

  1. 添加依赖到requirements.txt

    pysqlite3-binary>=0.4.10
    
  2. 修改Django的settings.py
    在文件最顶部添加以下代码,让Django使用pysqlite3替代内置模块:

    __import__('pysqlite3')
    import sys
    sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
    
  3. 确保buildspec中先安装依赖
    在install阶段执行pip install -r requirements.txt,确保pysqlite3被安装。

内容的提问来源于stack exchange,提问作者alphazwest

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:35:17