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

Termux中使用pyenv安装Python 3.8.13遇依赖及编译错误

在Termux中使用pyenv安装Python 3.8.13的问题解决

问题背景

按照pyenv官方指引安装编译依赖时,多个包无法找到或无安装候选;替换依赖包后执行pyenv install 3.8.13,出现编译错误。

1. 依赖安装失败情况

执行官方推荐的依赖安装命令:

apt-get update; apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

得到错误输出:

Hit:1 https://mirrors.nju.edu.cn/termux/apt/termux-main stable InRelease
Hit:2 https://mirrors.nju.edu.cn/termux/apt/termux-root root InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package libbz2-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  libbz2

Package libffi-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  libffi

Package liblzma-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  liblzma

Package libxml2-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  libxml2

E: Unable to locate package libssl-dev
E: Unable to locate package zlib1g-dev
E: Package 'libbz2-dev' has no installation candidate
E: Unable to locate package libreadline-dev
E: Unable to locate package libsqlite3-dev
E: Unable to locate package libncursesw5-dev
E: Unable to locate package tk-dev
E: Package 'libxml2-dev' has no installation candidate
E: Unable to locate package libxmlsec1-dev
E: Package 'libffi-dev' has no installation candidate
E: Package 'liblzma-dev' has no installation candidate

2. 替代依赖安装

使用以下命令安装替代依赖:

apt install make build-essential zlib libbz2 libsqlite wget curl llvm ncurses* xz-utils tk libxml2 xmlsec libffi liblzma

3. Python编译错误

安装依赖后执行pyenv install 3.8.13,出现编译错误:

/data/data/com.termux/files/usr/tmp/python-build.20220727123107.25556 ~
/data/data/com.termux/files/usr/tmp/python-build.20220727123107.25556/Python-3.8.13 /data/data/com.termux/files/usr/tmp/python-build.20220727123107.25556 ~
checking build system type... aarch64-unknown-linux-gnu
checking host system type... aarch64-unknown-linux-gnu
checking for python3.8... no
···
Python/bootstrap_hash.c:131:17: error: implicit declaration of function 'getrandom' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            n = getrandom(dest, n, flags);
                ^
Python/bootstrap_hash.c:135:17: error: implicit declaration of function 'getrandom' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            n = getrandom(dest, n, flags);
                ^
2 errors generated.
make: *** [Makefile:1730: Python/bootstrap_hash.o] Error 1
make: *** Waiting for unfinished jobs....
Python/pytime.c:154:10: warning: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Wimplicit-const-int-float-conversion]
    if (!_Py_InIntegralTypeRange(time_t, intpart)) {
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./Include/pymath.h:228:82: note: expanded from macro '_Py_InIntegralTypeRange'
#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type))
                                                                              ~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
./Include/pymath.h:221:124: note: expanded from macro '_Py_IntegralTypeMax'
#define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
                                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
Python/pytime.c:207:14: warning: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Wimplicit-const-int-float-conversion]
        if (!_Py_InIntegralTypeRange(time_t, intpart)) {
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./Include/pymath.h:228:82: note: expanded from macro '_Py_InIntegralTypeRange'
#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type))
                                                                              ~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
./Include/pymath.h:221:124: note: expanded from macro '_Py_IntegralTypeMax'
#define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
                                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
Python/pytime.c:392:10: warning: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Wimplicit-const-int-float-conversion]
    if (!_Py_InIntegralTypeRange(_PyTime_t, d)) {
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./Include/pymath.h:228:82: note: expanded from macro '_Py_InIntegralTypeRange'
#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type))
                                                                              ~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
./Include/pymath.h:221:124: note: expanded from macro '_Py_IntegralTypeMax'
#define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
                                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
3 warnings generated.

解决方案

1. 安装正确的Termux依赖包

Termux的包命名规则与Debian系不同,部分开发文件直接包含在主包中,使用以下命令安装完整依赖:

apt update && apt install make build-essential openssl-dev zlib libbz2 readline sqlite wget curl llvm ncurses xz-utils tk libxml2 xmlsec libffi liblzma

2. 解决getrandom编译错误

getrandom是GNU扩展函数,Python 3.8在Termux环境下需要显式定义_GNU_SOURCE宏才能识别该函数,执行安装命令时添加编译参数:

CFLAGS="-D_GNU_SOURCE" pyenv install 3.8.13

执行上述命令后,Python 3.8.13即可正常编译安装。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 04:24:11