Python cProfile:pyo 文件是否可能
我是Python初学者,尝试通过命令行调用cProfile,
python -m cProfile -o ./temp/PROFILE.log myScript.pyo
但它会抛出一条错误消息,指出
SyntaxError: Non-ASCII character '\xb3' in file myScript.pyo on line 1, but no encoding statements;请参阅http://www.python.org/peps/pep-0263.html 详细信息
但如果我对 myScript.py 文件执行相同的操作,它就可以正常工作。
我必须在客户端计算机上收集配置文件数据,并且该计算机上无法拥有源代码。
我有什么遗漏的吗?
I am beginner to Python and trying to invoke the cProfile through command line i.e.
python -m cProfile -o ./temp/PROFILE.log myScript.pyo
But it throws an error message stating that
SyntaxError: Non-ASCII character '\xb3' in file myScript.pyo on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
But if I do the same for myScript.py file, it works fine.
I have to gather the profile data on a client machine and cannot have source code on that machine.
Is there something I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定 cProfile 使用了 execfile()。该提示来自文档( http://docs.python.org/library/profile.html ):
execfile() 无法执行 *.pyc 和 *.pyo 文件 - 它失败并出现相同的异常。
针对 .pyc 或 .pyo 运行 profile/cProfile 的用例可能从未被解决。我还没有找到关于为什么会出现这种情况的规范解释,但由于 cPython 解释器的工作方式,通常预计主脚本不会被字节编译。解释器不会自动对主脚本进行字节编译,但会对导入的模块进行字节编译。这是关于该主题的一个问题: 为什么Python 会编译模块但不编译正在运行的脚本吗?
要解决您的问题,您可以启动脚本来调用您通常在要分析的 .pyo 中作为 main 执行的内容。公开的代码将非常简单。
launch.py
然后运行:
I'm pretty sure that cProfile makes use of execfile(). The hint comes from the docs ( http://docs.python.org/library/profile.html ):
execfile() is unable to execute *.pyc and *.pyo files - it fails with the same exception.
The use case of running profile/cProfile against an .pyc or .pyo was probably just never addressed. I haven't found a canonical explanation for why it's the case, but main scripts are generally expected to not be byte-compiled because of the way that the cPython interpreter works. The interpreter will not automatically byte-compile main scripts, but will do so for imported modules. Here is an SO Question on the topic: Why does Python compile modules but not the script being run?
To workaround your issue, you could have launch scripts to call what you would normally have executed as main in the .pyo that you would like to profile. The exposed code would be pretty trivial.
launch.py
And then run: