Skip to content

Commit 47f67f8

Browse files
committed
tools: Prefer python3 during install and export
Install and export script should work on systems without "python" executable. Closes #6471 Closes #6532 Related to #6421 and espressif/arduino-esp32#4717
1 parent e22de81 commit 47f67f8

File tree

11 files changed

+90
-100
lines changed

11 files changed

+90
-100
lines changed

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

+1-28
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ To compile with ESP-IDF you need to get the following packages. The command to r
1515

1616
- CentOS 7 & 8::
1717

18-
sudo yum -y update && sudo yum install git wget flex bison gperf python3 cmake ninja-build ccache dfu-util libusbx
18+
sudo yum -y update && sudo yum install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache dfu-util libusbx
1919

2020
CentOS 7 is still supported but CentOS version 8 is recommended for a better user experience.
2121

@@ -35,33 +35,6 @@ Permission issues /dev/ttyUSB0
3535

3636
With some Linux distributions you may get the ``Failed to open port /dev/ttyUSB0`` error message when flashing the {IDF_TARGET_NAME}. :ref:`This can be solved by adding the current user to the dialout group<linux-dialout-group>`.
3737

38-
Setting up Python 3 as default for CentOS
39-
-----------------------------------------
40-
41-
CentOS 7 and older is providing Python 2.7 as the default interpreter.
42-
Python 3 is recommended instead and can be installed in old distributions as follows, or please consult the documentation of your operating system for other recommended ways to achieve this::
43-
44-
sudo yum -y update && sudo yum install python3 python3-pip python3-setuptools
45-
46-
Making Python 3 the default interpreter is possible by running::
47-
48-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
49-
50-
Setting up Python 3 as default for Ubuntu and Debian
51-
----------------------------------------------------
52-
53-
Ubuntu (version 18.04 and older) and Debian (version 9 and older) are still providing Python 2.7 as the default interpreter.
54-
Python 3 is recommended instead and can be installed in old distributions as follows, or please consult the documentation of your operating system for other recommended ways to achieve this::
55-
56-
sudo apt-get install python3 python3-pip python3-setuptools
57-
58-
Making Python 3 the default interpreter is possible by running::
59-
60-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
61-
62-
.. note::
63-
This is system-wide change which may affect all of the applications.
64-
6538
Fixing broken pip on Ubuntu 16.04
6639
=================================
6740

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

+3-18
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ ESP-IDF will use the version of Python installed by default on macOS.
3434

3535
Then you will need to install the XCode command line tools to continue. You can install these by running ``xcode-select --install``.
3636

37-
Installing and setting up Python 3 as default
38-
---------------------------------------------
37+
Installing Python 3
38+
-------------------
3939

4040
Basing on macOS `Catalina 10.15 release notes`_, use of Python 2.7 is not recommended and Python 2.7 will not be included by default in future versions of macOS. Check what Python you currently have::
4141

@@ -47,30 +47,15 @@ If the output is like ``Python 2.7.17``, your default interpreter is Python 2.7.
4747

4848
If above command returns an error, it means Python 3 is not installed.
4949

50-
Below is an overview of steps to install Python 3 and making it default interpreter.
50+
Below is an overview of steps to install Python 3.
5151

5252
- Installing with HomeBrew_ can be done as follows::
5353

5454
brew install python3
55-
ln -s /usr/local/bin/python3.8 /usr/local/bin/python
56-
57-
Adjust above directory name ``/usr/local/bin/python3.8`` to point where Python 3 has been installed. To check this directory you can run ``which -a python3``.
5855

5956
- If you have MacPorts_, you can run::
6057

6158
sudo port install python38
62-
sudo port select --set python python38
63-
64-
Now open a new terminal session to verify what python is running by default::
65-
66-
python --version
67-
68-
If the output is similar to ``Python 3.8.5``, your installation has been done successfully.
69-
70-
71-
.. note::
72-
73-
This is system-wide change which may affect all of the applications.
7459

7560
Python 2 deprecation
7661
====================

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

+1-28
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Linux 平台工具链的标准设置
1515

1616
- CentOS 7 & 8::
1717

18-
sudo yum -y update && sudo yum install git wget flex bison gperf python3 cmake ninja-build ccache dfu-util libusbx
18+
sudo yum -y update && sudo yum install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache dfu-util libusbx
1919

2020
目前仍然支持 CentOS 7,但为了更好的用户体验,建议使用 CentOS 8。
2121

@@ -35,33 +35,6 @@ Linux 平台工具链的标准设置
3535

3636
使用某些 Linux 版本向 {IDF_TARGET_NAME} 烧录固件时,可能会出现 ``Failed to open port /dev/ttyUSB0`` 错误消息。此时可以将用户添加至 :ref:`Linux Dialout 组<linux-dialout-group>`。
3737

38-
设置 Python 3 为 CentOS 默认 Python 版本
39-
----------------------------------------------------
40-
41-
CentOS 7 及更早的版本提供 Python 2.7 作为默认解释器。但这里推荐使用 Python 3,您可以运行下方命令安装 Python 3。或者查看当前所用系统的相关文档,按照文档推荐的其它方法安装 Python 3::
42-
43-
sudo yum -y update && sudo yum install python3 python3-pip python3-setuptools
44-
45-
设置 Python 3 为默认 Python 版本::
46-
47-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
48-
49-
50-
设置 Python 3 为 Ubuntu 和 Debian 默认 Python 版本
51-
----------------------------------------------------
52-
53-
Ubuntu(v18.04 及之前的版本)和 Debian(v9 及之前的版本)的默认解释器为 Python 2.7,但这里推荐使用 Python 3,您可以运行下方命令安装 Python 3。或者查看当前所用系统的相关文档,按照文档推荐的其它方法安装 Python 3::
54-
55-
sudo apt-get install python3 python3-pip python3-setuptools
56-
57-
设置 Python 3 为默认 Python 版本::
58-
59-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
60-
61-
.. 注解::
62-
上述设置为全局设置,同时会影响到其它应用。
63-
64-
6538
修复 Ubuntu 16.04 损坏的 pip
6639
=================================
6740

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

+3-20
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ ESP-IDF 将使用 macOS 上默认安装的 Python 版本。
2727

2828
- 强烈建议同时安装 ccache_ 以获得更快的编译速度。如有 HomeBrew_,可通过 MacPorts_ 上的 ``brew install ccache`` 或 ``sudo port install ccache`` 完成安装。
2929

30-
.. note::
31-
30+
.. note::
3231
如您在上述任何步骤中遇到以下错误::
3332

3433
``xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at:/Library/Developer/CommandLineTools/usr/bin/xcrun``
3534

3635
则必须安装 XCode 命令行工具,具体可运行 ``xcode-select --install``。
3736

38-
安装并设置 Python 3 为默认版本
37+
安装 Python 3
3938
---------------------------------------------
4039

4140
`Catalina 10.15 发布说明`_ 中表示不推荐使用 Python 2.7 版本,在未来的 macOS 版本中也不会默认包含 Python 2.7。执行以下命令来检查您当前使用的 Python 版本::
@@ -48,31 +47,15 @@ ESP-IDF 将使用 macOS 上默认安装的 Python 版本。
4847

4948
如果运行上述命令出现错误,则代表电脑上没有安装 Python 3。
5049

51-
请根据以下步骤安装 Python 3 并使其成为默认解释器
50+
请根据以下步骤安装 Python 3:
5251

5352
- 使用 HomeBrew_ 进行安装的方法如下::
5453

5554
brew install python3
56-
ln -s /usr/local/bin/python3.8 /usr/local/bin/python
57-
58-
将上述的目录名 ``/usr/local/bin/python3.8`` 修改为 Python 3 所在的目录。您可以运行 ``which -a python3`` 来查看 Python 3 所在的目录。
5955

6056
- 使用 MacPorts_ 进行安装的方法如下::
6157

6258
sudo port install python38
63-
sudo port select --set python python38
64-
65-
现在您可以打开终端窗口验证默认运行的 Python 版本::
66-
67-
python --version
68-
69-
如果输出结果类似于 ``Python 3.8.5`` 则代表安装成功。
70-
71-
.. 注解::
72-
73-
上述设置为全局设置,同时会影响到其它应用。
74-
75-
7659

7760
停用 Python 2
7861
====================

export.fish

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ function idf_export_main
88

99
set oldpath = $PATH
1010

11+
echo "Detecting the Python interpreter"
12+
source "$IDF_PATH"/tools/detect_python.fish
13+
1114
echo "Adding ESP-IDF tools to PATH..."
1215
# Call idf_tools.py to export tool paths
1316
set -x IDF_TOOLS_EXPORT_CMD "$IDF_PATH"/export.fish
1417
set -x IDF_TOOLS_INSTALL_CMD "$IDF_PATH"/install.fish
15-
set idf_exports ("$IDF_PATH"/tools/idf_tools.py export) || return 1
18+
set idf_exports ("$ESP_PYTHON" "$IDF_PATH"/tools/idf_tools.py export) || return 1
1619
eval "$idf_exports"
1720

1821
echo "Checking if Python packages are up to date..."
@@ -50,6 +53,7 @@ function idf_export_main
5053
set -e path_entry
5154
set -e IDF_ADD_PATHS_EXTRAS
5255
set -e idf_exports
56+
set -e ESP_PYTHON
5357

5458
# Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
5559
# to check whether we are using a private Python environment

export.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ idf_export_main() {
7979

8080
old_path="$PATH"
8181

82+
echo "Detecting the Python interpreter"
83+
. "${IDF_PATH}/tools/detect_python.sh"
84+
8285
echo "Adding ESP-IDF tools to PATH..."
8386
# Call idf_tools.py to export tool paths
8487
export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
8588
export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
86-
idf_exports=$("${IDF_PATH}/tools/idf_tools.py" export) || return 1
89+
idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1
8790
eval "${idf_exports}"
8891

8992
echo "Using Python interpreter in $(which python)"
@@ -125,6 +128,7 @@ idf_export_main() {
125128
unset path_entry
126129
unset IDF_ADD_PATHS_EXTRAS
127130
unset idf_exports
131+
unset ESP_PYTHON
128132

129133
# Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
130134
# to check whether we are using a private Python environment

install.fish

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ set basedir $PWD
44

55
set -x IDF_PATH $basedir
66

7+
echo "Detecting the Python interpreter"
8+
source "$IDF_PATH"/tools/detect_python.fish
9+
710
echo "Installing ESP-IDF tools"
8-
"$IDF_PATH"/tools/idf_tools.py install
11+
"$ESP_PYTHON" "$IDF_PATH"/tools/idf_tools.py install
912

1013
echo "Installing Python environment and packages"
11-
"$IDF_PATH"/tools/idf_tools.py install-python-env
14+
"$ESP_PYTHON" "$IDF_PATH"/tools/idf_tools.py install-python-env
1215

1316
echo "All done! You can now run:"
1417
echo ""

install.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ set -u
55

66
export IDF_PATH=$(cd $(dirname $0); pwd)
77

8+
echo "Detecting the Python interpreter"
9+
. ${IDF_PATH}/tools/detect_python.sh
10+
811
echo "Installing ESP-IDF tools"
9-
${IDF_PATH}/tools/idf_tools.py install
12+
${ESP_PYTHON} ${IDF_PATH}/tools/idf_tools.py install
1013

1114
echo "Installing Python environment and packages"
12-
${IDF_PATH}/tools/idf_tools.py install-python-env
15+
${ESP_PYTHON} ${IDF_PATH}/tools/idf_tools.py install-python-env
1316

1417
basedir="$(dirname $0)"
1518
echo "All done! You can now run:"

tools/ci/config/host-test.yml

+13
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,16 @@ test_autocomplete:
344344
expire_in: 1 week
345345
script:
346346
- ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh ${IDF_PATH}/tools/ci/test_autocomplete.py
347+
348+
test_detect_python:
349+
extends: .host_test_template
350+
image: $CI_DOCKER_REGISTRY/linux-shells:1
351+
script:
352+
- cd ${IDF_PATH}
353+
- shellcheck -s sh tools/detect_python.sh
354+
- shellcheck -s bash tools/detect_python.sh
355+
- shellcheck -s dash tools/detect_python.sh
356+
- "bash -c '. tools/detect_python.sh && echo Our Python: ${ESP_PYTHON?Python is not set}'"
357+
- "dash -c '. tools/detect_python.sh && echo Our Python: ${ESP_PYTHON?Python is not set}'"
358+
- "zsh -c '. tools/detect_python.sh && echo Our Python: ${ESP_PYTHON?Python is not set}'"
359+
- "fish -c 'source tools/detect_python.fish && echo Our Python: $ESP_PYTHON'"

tools/detect_python.fish

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file should be sourced, not executed!
2+
#
3+
# This is a port of detect_python.sh. More information are provided there.
4+
5+
set -x ESP_PYTHON python
6+
7+
for p_cmd in python python3;
8+
echo "Checking \"$p_cmd\" ..."
9+
10+
set res ($p_cmd -c "import sys; print(sys.version_info.major)")
11+
if [ "$res" = "3" ]
12+
set -x ESP_PYTHON $p_cmd
13+
break
14+
end
15+
end
16+
17+
$ESP_PYTHON --version
18+
if [ $status -ne 0 ]
19+
echo "\"$ESP_PYTHON\" is not installed! Please see the documentation for how to install it."
20+
# The following exit skips the rest of this file but won't exit fish where the script was sourced. This is not a
21+
# fatal issue.
22+
exit 1
23+
end
24+
25+
echo "\"$ESP_PYTHON\" has been detected"

tools/detect_python.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This file should be sourced, not executed!
2+
#
3+
# This is a helper script for detecting Python executables in the PATH. It is intended to be used for determining
4+
# which Python should be used with idf_tools.py for installing tools and exporting environment variables.
5+
#
6+
# 1. The script will set variable ESP_PYTHON to "python" if it is of version 3.
7+
# 2. Otherwise, "python3" will be exported if it exists.
8+
# 3. The script will fall-back to "python" as the last resort and fail if it doesn't exist.
9+
10+
ESP_PYTHON=python
11+
12+
for p_cmd in python python3
13+
do
14+
echo "Checking \"$p_cmd\" ..."
15+
16+
if [ "$($p_cmd -c "import sys; print(sys.version_info.major)")" = 3 ]; then
17+
ESP_PYTHON=$p_cmd
18+
break
19+
fi
20+
done
21+
22+
$ESP_PYTHON --version || { echo "\"$ESP_PYTHON\" is not installed! Please see the documentation for how to install it."; exit 1; }
23+
24+
echo "\"$ESP_PYTHON\" has been detected"

0 commit comments

Comments
 (0)