Skip to content

Commit b086a41

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 857b34c commit b086a41

File tree

8 files changed

+54
-98
lines changed

8 files changed

+54
-98
lines changed

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

+1-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To compile with ESP-IDF you need to get the following packages:
1111

1212
- CentOS 7::
1313

14-
sudo yum -y update && sudo yum install git wget flex bison gperf python3 cmake ninja-build ccache
14+
sudo yum -y update && sudo yum install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache
1515

1616
CentOS 7 is still supported but CentOS version 8 is recommended for a better user experience.
1717

@@ -48,33 +48,6 @@ Before installing these packages you might need to add the author's public key t
4848

4949
Alternatively, use crosstool-NG to compile a gdb that links against ncurses 6.
5050

51-
Setting up Python 3 as default for CentOS
52-
-----------------------------------------
53-
54-
CentOS 7 and older is providing Python 2.7 as the default interpreter.
55-
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::
56-
57-
sudo yum -y update && sudo yum install python3 python3-pip python3-setuptools
58-
59-
Making Python 3 the default interpreter is possible by running::
60-
61-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
62-
63-
Setting up Python 3 as default for Ubuntu and Debian
64-
----------------------------------------------------
65-
66-
Ubuntu (version 18.04 and older) and Debian (version 9 and older) are still providing Python 2.7 as the default interpreter.
67-
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::
68-
69-
sudo apt-get install python3 python3-pip python3-setuptools
70-
71-
Making Python 3 the default interpreter is possible by running::
72-
73-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
74-
75-
.. note::
76-
This is system-wide change which may affect all of the applications.
77-
7851
Fixing broken pip on Ubuntu 16.04
7952
=================================
8053

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-29
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Linux 平台工具链的标准设置
1111

1212
- CentOS 7::
1313

14-
sudo yum -y update && sudo yum install git wget flex bison gperf python3 cmake ninja-build ccache
14+
sudo yum -y update && sudo yum install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache
1515

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

@@ -49,34 +49,6 @@ Arch Linux 用户
4949

5050
此外,您也可以使用 crosstool-NG 编译一个链接到 ncurses 6 的 gdb。
5151

52-
设置 Python 3 为 CentOS 默认 Python 版本
53-
----------------------------------------------------
54-
55-
CentOS 7 及更早的版本提供 Python 2.7 作为默认解释器。但这里推荐使用 Python 3,您可以运行下方命令安装 Python 3。或者查看当前所用系统的相关文档,按照文档推荐的其它方法安装 Python 3::
56-
57-
sudo yum -y update && sudo yum install python3 python3-pip python3-setuptools
58-
59-
设置 Python 3 为默认 Python 版本::
60-
61-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
62-
63-
64-
设置 Python 3 为 Ubuntu 和 Debian 默认 Python 版本
65-
----------------------------------------------------
66-
67-
68-
Ubuntu(v18.04 及之前的版本)和 Debian(v9 及之前的版本)的默认解释器为 Python 2.7,但这里推荐使用 Python 3,您可以运行下方命令安装 Python 3。或者查看当前所用系统的相关文档,按照文档推荐的其它方法安装 Python 3::
69-
70-
sudo apt-get install python3 python3-pip python3-setuptools
71-
72-
设置 Python 3 为默认 Python 版本::
73-
74-
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3
75-
76-
77-
.. 注解::
78-
上述设置为全局设置,同时会影响到其它应用。
79-
8052
修复 Ubuntu 16.04 损坏的 pip
8153
=================================
8254

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

+3-20
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ ESP-IDF 将使用 Mac OS 上默认安装的 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 将使用 Mac OS 上默认安装的 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.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 "Checking if Python packages are up to date..."
@@ -123,6 +126,7 @@ idf_export_main() {
123126
unset path_entry
124127
unset IDF_ADD_PATHS_EXTRAS
125128
unset idf_exports
129+
unset ESP_PYTHON
126130

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

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

+12
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,15 @@ check_doc_links:
285285
- test "${CI_COMMIT_REF_NAME}" = "master" || exit 0
286286
- cd docs
287287
- make linkcheck
288+
289+
test_detect_python:
290+
extends: .host_test_template
291+
image: $CI_DOCKER_REGISTRY/linux-shells:1
292+
script:
293+
- cd ${IDF_PATH}
294+
- shellcheck -s sh tools/detect_python.sh
295+
- shellcheck -s bash tools/detect_python.sh
296+
- shellcheck -s dash tools/detect_python.sh
297+
- "bash -c '. tools/detect_python.sh && echo Our Python: ${ESP_PYTHON?Python is not set}'"
298+
- "dash -c '. tools/detect_python.sh && echo Our Python: ${ESP_PYTHON?Python is not set}'"
299+
- "zsh -c '. tools/detect_python.sh && echo Our Python: ${ESP_PYTHON?Python is not set}'"

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)