首页 > 解决方案 > gn 脚本使用错误的 Python 版本运行(“需要类似字节的对象,而不是 'str'”)

问题描述

在 macOS(Catalina 10.15)上构建 Chromium 或 libwebrtc 时gn,我从 Python 构建脚本中收到关于bytesstr. 例如:

src [heads/master●] % gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64"'
ERROR at //build/config/ios/ios_sdk.gni:109:21: Script returned non-zero exit code.
  _ios_sdk_result = exec_script(script_name, ios_sdk_info_args, "scope")
                    ^----------
Current dir: /Users/lynn/code/webrtc_ios/src/out/ios_64/
Command: python /Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py --get_sdk_info iphoneos
Returned 1.
stderr:

Traceback (most recent call last):
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 107, in <module>
    FillXcodeVersion(settings, args.developer_dir)
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 59, in FillXcodeVersion
    settings['xcode_version'] = FormatVersion(lines[0].split()[-1])
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 43, in FormatVersion
    major, minor, patch = SplitVersion(version)
  File "/Users/lynn/code/webrtc_ios/src/build/config/mac/sdk_info.py", line 30, in SplitVersion
    version = version.split('.')
TypeError: a bytes-like object is required, not 'str'

See //build/config/sysroot.gni:67:3: whence it was imported.
  import("//build/config/ios/ios_sdk.gni")
  ^--------------------------------------
See //build/config/linux/pkg_config.gni:5:1: whence it was imported.
import("//build/config/sysroot.gni")
^----------------------------------
See //BUILD.gn:15:1: whence it was imported.
import("//build/config/linux/pkg_config.gni")
^-------------------------------------------

当我将有问题的 Python 脚本编辑为 时print(sys.version),它显示它正在运行 Python 3,即使脚本应该在.vpython.

如何配置gn以使用适当的 Python 版本运行这些脚本?

标签: chromiumbuild-systembuild-scriptgn

解决方案


似乎exec_script正在使用机器 Python 版本运行这些脚本。gn help exec_script说:

默认的脚本解释器是 Python(在 POSIX 上为“python”,在 Windows 上为“python.exe”或“python.bat”)。这可以通过 script_executable 变量进行配置,参见“​​gn help dotfile”。

对我来说,python指向 Python 3。所以我不得不将此行添加到 .gn 点文件的末尾:

script_executable = "vpython"

现在构建使用 中定义的虚拟 Python .vpython,即 Python 2.7。

vpython可执行文件由 Chromium depot_tools 提供,就像gn.)


推荐阅读