QPython3中标准库模块__doc__无输出的问题求助
解决QPython中标准库模块
__doc__为空的问题 我之前在移动端Python环境里也碰到过类似的情况,你在Android的QPython(Python 3.2.2版本)里遇到的这个问题,本质是移动端Python发行版为了压缩安装包体积,默认剥离了标准库模块的__doc__属性,但help()能正常工作是因为它调用了内置帮助系统,会从预编译的帮助数据源读取内容。给你几个可行的解决思路:
1. 用inspect.getdoc()替代直接访问__doc__
inspect模块的getdoc()函数比直接读取__doc__更智能,它会尝试从多个渠道提取文档,包括help()依赖的底层数据源,刚好能绕过QPython剥离__doc__的限制。
示例代码:
import inspect import os # 以os模块为例 print(inspect.getdoc(os))
2. 切换到带完整文档的QPython版本
你当前使用的是Python 3.2.2的旧版QPython,这类早期移动端Python发行版为了节省存储空间,普遍会移除文档字符串。你可以尝试更新到QPython的新版本(比如支持Python 3.8及以上的版本),新版本通常会保留标准库的完整文档字符串,整体兼容性也更好。
3. 手动赋值文档字符串(极端方案)
如果上面的方法都没法落地,你可以从Python官方文档中复制对应模块的文档字符串,手动赋值给模块的__doc__属性。比如:
import sys sys.__doc__ = """This module provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter. Dynamic objects: argv -- command line arguments; argv[0] is the script pathname if known path -- module search path; path[0] is the script directory, else '' modules -- dictionary of loaded modules displayhook -- called to show results in an interactive session excepthook -- called to handle any uncaught exception other than SystemExit To customize printing in an interactive session or to install a custom top-level exception handler, assign other functions to replace these. ...(省略剩余官方文档内容) """
另外你之前运行的qpython-android5.sh只是启动了QPython的运行环境,并没有修改Python的编译配置,所以没法恢复被移除的文档字符串,这也是脚本没起作用的原因。
内容的提问来源于stack exchange,提问作者Xantium
相关产品推荐
相关产品推荐

