Skip to content

Commit c2a568f

Browse files
committed
feat(tools): allow to specify python binary by ESP_PYTHON environment variable
1 parent 8f1e7bc commit c2a568f

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

docs/en/api-guides/tools/idf-tools.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Shell-specific user-facing installation scripts are provided in the root directo
161161
* ``install.sh`` for Bash
162162
* ``install.fish`` for Fish
163163

164-
Apart from downloading and installing the tools in ``IDF_TOOLS_PATH``, these scripts prepare a Python virtual environment, and install the required packages into that environment.
164+
Apart from downloading and installing the tools in ``IDF_TOOLS_PATH``, these scripts prepare a Python virtual environment, and install the required packages into that environment. If you want to specify a particular Python for ESP-IDF, you need to set environment variable ``ESP_PYTHON`` before installation and each time before use. For example ``export ESP_PYTHON=/opt/bin/python3.13``.
165165

166166
These scripts accept optionally a comma-separated list of chip targets and ``--enable-*`` arguments for enabling features. These arguments are passed to the ``idf_tools.py`` script which stores them in ``idf-env.json``. Therefore, chip targets and features can be enabled incrementally.
167167

docs/en/get-started/linux-macos-setup.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ To install supported Python 3 on macOS:
116116

117117
During installation, the install script will check for supported Python versions on your system and select the oldest version that meets the minimum requirement.
118118

119+
If you want to specify a particular Python for ESP-IDF, you need to set environment variable ``ESP_PYTHON`` before installation and each time before use. For example ``export ESP_PYTHON=/opt/bin/python3.13``.
120+
119121
.. _get-started-get-esp-idf:
120122

121123
Step 2. Get ESP-IDF

docs/zh_CN/api-guides/tools/idf-tools.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ ESP-IDF 的根目录中提供了针对不同 shell 的用户安装脚本,包
161161
* ``install.sh`` 适用于 Bash
162162
* ``install.fish`` 适用于 Fish
163163

164-
这些脚本除了下载和安装 ``IDF_TOOLS_PATH`` 中的工具外,还会准备一个 Python 虚拟环境,并在此虚拟环境中安装所需软件包。
164+
这些脚本除了下载和安装 ``IDF_TOOLS_PATH`` 中的工具外,还会准备一个 Python 虚拟环境,并在此虚拟环境中安装所需软件包。如果要指定 ESP-IDF 使用特定的 Python ,则需要在安装之前及每次使用之前设置环境变量 ``ESP_PYTHON``,例如 ``export ESP_PYTHON=/opt/bin/python3.13``。
165165

166166
为启用相应功能,这些脚本可以选择性地接受一组以逗号分隔的芯片目标列表及 ``--enable-*`` 参数,这类参数会传递给 ``idf_tools.py`` 脚本,并由该脚本将这类参数存储在 ``idf-env.json`` 中,从而逐步启用芯片目标及功能。
167167

docs/zh_CN/get-started/linux-macos-setup.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Apple M1 用户
115115
.. note::
116116

117117
安装过程中,安装脚本 (install.sh) 会检查系统中已安装的 Python 版本,并在所有符合最低要求的版本中,选择最早的版本使用。
118+
119+
如果要指定 ESP-IDF 使用特定的 Python ,则需要在安装之前及每次使用之前设置环境变量 ``ESP_PYTHON``,例如 ``export ESP_PYTHON=/opt/bin/python3.13``。
118120

119121
.. _get-started-get-esp-idf:
120122

tools/detect_python.fish

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@
55
set OLDEST_PYTHON_SUPPORTED_MAJOR 3
66
set OLDEST_PYTHON_SUPPORTED_MINOR 10
77

8-
set -x ESP_PYTHON python
8+
if test -z "$ESP_PYTHON"
9+
set PYTHON_CANDIDATES python3 python python3.10 python3.11 python3.12 python3.13
10+
else
11+
echo "Reading ESP_PYTHON from environment: \"$ESP_PYTHON\""
12+
set PYTHON_CANDIDATES $ESP_PYTHON
13+
end
14+
set -e ESP_PYTHON # Unset it. Will assign after candidate check
915

10-
for p_cmd in python3 python python3.10 python3.11 python3.12 python3.13;
16+
for p_cmd in $PYTHON_CANDIDATES
1117
$p_cmd --version >/dev/null 2>&1; or continue
1218
echo "Checking \"$p_cmd\" ..."
1319

1420
$p_cmd -c "import sys; exit(1) if sys.version_info.major < int(\"$OLDEST_PYTHON_SUPPORTED_MAJOR\") else exit(0);"; or continue
1521
$p_cmd -c "import sys; exit(1) if sys.version_info.minor < int(\"$OLDEST_PYTHON_SUPPORTED_MINOR\") else exit(0);"; or continue
1622

17-
set ESP_PYTHON $p_cmd
23+
set -x ESP_PYTHON $p_cmd
1824
break
1925
end
26+
set -e PYTHON_CANDIDATES
2027

21-
test -n "$ESP_PYTHON"; or echo "Python $OLDEST_PYTHON_SUPPORTED_MAJOR.$OLDEST_PYTHON_SUPPORTED_MINOR+ is not installed! Please see the documentation for how to install it."
28+
test -n "$ESP_PYTHON"; or echo "Python $OLDEST_PYTHON_SUPPORTED_MAJOR.$OLDEST_PYTHON_SUPPORTED_MINOR+ is not installed! Please see the documentation for how to install it." >&2
2229
test -n "$ESP_PYTHON"; or exit 1
2330

2431
$ESP_PYTHON --version

tools/detect_python.sh

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,36 @@
33
# This is a helper script for detecting Python executables in the PATH. It is intended to be used for determining
44
# which Python should be used with idf_tools.py for installing tools and exporting environment variables.
55
#
6-
# 1. The script is looking for python version same or greater than minimal required version on path
6+
# 1. If environment variable ESP_PYTHON already set, script uses that python binary, otherwise, looks for python version same or greater than minimal required version on path
77
# 2. If required version of python is found it is assigned to environmental variable `ESP_PYTHON`
88
# 3. If required version of python is not found, script will fail
99

1010
OLDEST_PYTHON_SUPPORTED_MAJOR=3
1111
OLDEST_PYTHON_SUPPORTED_MINOR=10
1212

13-
ESP_PYTHON=python
1413

15-
for p_cmd in python3 python python3.10 python3.11 python3.12 python3.13; do
16-
$p_cmd --version >/dev/null 2>&1 || continue
14+
if [[ -z "$ESP_PYTHON" ]]; then
15+
PYTHON_CANDIDATES=(python3 python python3.10 python3.11 python3.12 python3.13)
16+
else
17+
echo "Reading ESP_PYTHON from environment: \"$ESP_PYTHON\""
18+
PYTHON_CANDIDATES=("$ESP_PYTHON")
19+
fi
20+
ESP_PYTHON= # Unset it. Will assign after candidate check
21+
22+
for p_cmd in "${PYTHON_CANDIDATES[@]}"; do
23+
"$p_cmd" --version >/dev/null 2>&1 || continue
1724
echo "Checking \"$p_cmd\" ..."
1825

19-
$p_cmd -c "import sys; exit(1) if sys.version_info.major < int(\"$OLDEST_PYTHON_SUPPORTED_MAJOR\") else exit(0);" || continue
20-
$p_cmd -c "import sys; exit(1) if sys.version_info.minor < int(\"$OLDEST_PYTHON_SUPPORTED_MINOR\") else exit(0);" || continue
26+
"$p_cmd" -c "import sys; exit(1) if sys.version_info.major < int(\"$OLDEST_PYTHON_SUPPORTED_MAJOR\") else exit(0);" || continue
27+
"$p_cmd" -c "import sys; exit(1) if sys.version_info.minor < int(\"$OLDEST_PYTHON_SUPPORTED_MINOR\") else exit(0);" || continue
2128

22-
ESP_PYTHON=$p_cmd
29+
export ESP_PYTHON="$p_cmd"
2330
break
2431
done
32+
unset PYTHON_CANDIDATES
2533

26-
$ESP_PYTHON --version 2>/dev/null || {
27-
echo "Python ${OLDEST_PYTHON_SUPPORTED_MAJOR}.${OLDEST_PYTHON_SUPPORTED_MINOR}+ is not installed! Please see the documentation for how to install it."
34+
[[ -n "$ESP_PYTHON" ]] && "$ESP_PYTHON" --version 2>/dev/null || {
35+
echo "Python ${OLDEST_PYTHON_SUPPORTED_MAJOR}.${OLDEST_PYTHON_SUPPORTED_MINOR}+ is not installed! Please see the documentation for how to install it." >&2
2836
exit 1
2937
}
3038
echo "\"$ESP_PYTHON\" has been detected"

0 commit comments

Comments
 (0)