kali编译AOSP报错(SyntaxError: Missing parentheses in call to ‘print‘. Did you mean print(...)? )

SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(…)?

今天下午刚解决完上一个问题后,下一个问题在编译了一个半小时后又冒了出来

image-20241004183344275

wildcard(out/target/product/generic_x86_64/clean_steps.mk) was changed, regenerating...
[ 99% 11909/11910] Create system-qemu.img now
FAILED: out/target/product/generic_x86_64/system-qemu.img
/bin/bash -c "(export SGDISK=out/host/linux-x86/bin/sgdisk SIMG2IMG=out/host/linux-x86/bin/simg2img;      device/generic/goldfish/tools/mk_combined_img.py -i out/target/product/generic_x86_64/system-qemu-config.txt -o out/target/product/generic_x86_64/system-qemu.img)"
  File "/home/kali/aosp/device/generic/goldfish/tools/mk_combined_img.py", line 53
    print "Invalid partition number: %d, range [1..%d]" % \
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
[100% 11910/11910] build check-all-partition-sizes
The sum of sizes of [system vendor] is within BOARD_SUPER_PARTITION_SIZE:
1468014592+119820288 == 1587834880 <=  3229614080 == 3229614080
The sum of sizes of [system vendor] is within BOARD_EMULATOR_DYNAMIC_PARTITIONS_SIZE:
1468014592+119820288 == 1587834880 <= 3221225472 == 3221225472
The sum of sizes of [emulator_dynamic_partitions] is within BOARD_SUPER_PARTITION_SIZE:
3221225472 == 3221225472 <= 3229614080 == 3229614080
05:53:59 ninja failed with: exit status 1

#### failed to build some targets (08:34 (mm:ss)) ####

你的错误的具体位置可能不一样,但核心问题都是SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?,同样在网上查了一下,发现是python版本的问题

这个应该用python2,而不是python3

解决方案:

如果操作系统没有安装python2,那就安装python2(不过linux系统一般会预装python2)

然后修改报错对应的python文件:

sudo vim ~/aosp/device/generic/goldfish/tools/mk_combined_img.py

在图中所示位置:
image-20241004183730687

把第一行的python改成python2即可

结果:

image-20241004183805347

终于编译成功了

<think>好的,我现在要解决用户关于Python 3中print语法错误的问题。用户遇到了'SyntaxError: Missing parentheses in call to 'print'',根据提供的引用内容,这个问题主要是因为Python 2和Python 3的语法差异导致的。用户可能是在尝试运行旧版Python 2的代码,而环境是Python 3的。 首先,我需要确认用户的具体情况。他们是否在代码中使用了不带括号的print语句?比如print "hello"而不是print("hello")。根据引用[1]和[2],Python 3要求print必须使用括号,而Python 2则允许不带括号。因此,解决方法就是将print语句改为函数形式,加上括号。 接下来,用户可能还遇到了raw_input的问题,因为Python 3中raw_input已经被input取代。引用[2]中提到,用户需要将raw_input替换为input。这也是需要提醒用户的地方。 另外,用户提到双击运行脚本时出现闪退,引用[2]建议从命令行运行,这样可以查看错误信息。这一点很重要,因为GUI环境下错误信息可能一闪而过,难以捕捉。所以应该建议用户通过命令行执行脚本,以便更好地调试。 用户还可能混淆了Python 2和Python 3的解释器。比如,系统可能同时安装了Python 2和3,而用户可能错误地使用了python命令而不是python3。需要检查这一点,并建议使用明确的版本号或者确认环境变量设置正确。 此外,代码中可能有其他Python 2特有的语法,比如过时的异常处理(except Exception, e:而不是except Exception as e:)或者除法运算符的行为差异。虽然用户当前的问题集中在print,但可能需要全面检查代码兼容性。 最后,用户可能需要自动化工具来辅助转换代码,例如2to3工具,这可以自动修复大部分Python 2到3的语法问题。不过,手动检查仍然是必要的,以确保转换的正确性。 总结解决方案步骤:1. 修改print语句为函数形式;2. 替换raw_input为input;3. 使用命令行运行脚本以捕获错误;4. 确认使用正确的Python版本;5. 检查其他语法兼容性问题;6. 推荐使用2to3工具进行批量转换。</think>--- ### **Python 3中"Missing parentheses in call to 'print'"错误详解与修复** --- #### **1. 错误根源分析** Python 3对`print`语句进行了**语法标准化**,强制要求使用函数调用格式: ```python # Python 2语法(已废弃) print "Hello World" # 直接输出字符串 # Python 3语法(强制要求) print("Hello World") # 必须使用括号包裹参数[^1] ``` 若在Python 3中沿用Python 2的`print`语法,解释器会直接抛出`SyntaxError`(如引用[3]所示)。 --- #### **2. 核心修复方案** **步骤1:批量添加括号** 对代码中所有`print`语句进行语法改造: ```diff - print "Total: %d" % count + print("Total: %d" % count) - print "Processing...", + print("Processing...", end='') # 注意行尾逗号需替换为end参数 ``` **步骤2:处理多参数输出** Python 3中多个参数需统一传入函数: ```python # Python 2 print "Name:", name, "Age:", age # Python 3(自动添加空格分隔) print("Name:", name, "Age:", age) # Python 3(精确控制分隔符) print("Name: {}, Age: {}".format(name, age)) ``` --- #### **3. 关联语法迁移** 除`print`外,还需注意以下Python 2/3差异: | 语法元素 | Python 2示例 | Python 3适配方案 | |----------------|-----------------------|--------------------------| | 用户输入 | `raw_input()` | `input()`[^2] | | 整数除法 | `5 / 2 = 2` | `5 // 2 = 2` | | 异常处理 | `except Exception, e` | `except Exception as e` | | Unicode支持 | `str`类型非Unicode | `str`默认为Unicode | --- #### **4. 执行环境验证** **验证Python版本**: ```bash # 检查默认Python版本 python --version # 强制使用Python3解释器 python3 script.py ``` **命令行调试技巧**(如引用[2]建议): ```bash # Windows python -i script.py # 进入交互模式查看错误 # Linux/macOS PYTHONWARNINGS=all python3 script.py # 显示所有警告 ``` --- #### **5. 自动化迁移工具** 使用`2to3`工具批量转换旧代码: ```bash # 生成差异报告 2to3 script.py # 直接修改原文件 2to3 -w script.py ``` 该工具可自动修复以下问题: - `print`语句转函数 - `except`语法更新 - `xrange()`转`range()` - 模块导入路径调整 --- #### **6. 防御性编程实践** 在脚本开头添加版本检查: ```python import sys if sys.version_info.major < 3: raise RuntimeError("需要Python 3.6或更高版本") ``` --- ### **关键注意事项** 1. **文件编码声明**: Python 3默认使用UTF-8编码,若脚本含非ASCII字符需添加: ```python # -*- coding: utf-8 -*- ``` 2. **第三方库兼容性**: 使用`six`库实现版本兼容: ```python from six.moves import input # 兼容Py2/Py3的input ``` --- ### **相关问题** 1. 如何同时维护Python 2和3兼容的代码? 2. Python 3中`bytes`和`str`类型如何转换? 3. 为什么Python 3修改`print`语法设计? --- [^1]: Python 3通过强制函数调用规范,统一了语句与函数的行为边界 [^2]: `input()`在Python 3中完全替代了Python 2的`raw_input()`功能
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哪个编程语言更好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值