Python cProfile:pyo 文件是否可能

发布于 2024-11-18 16:51:09 字数 474 浏览 5 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白色秋天 2024-11-25 16:51:09

我很确定 cProfile 使用了 execfile()。该提示来自文档( http://docs.python.org/library/profile.html ):

该函数采用单个参数
可以传递给 exec
声明

execfile() 无法执行 *.pyc 和 *.pyo 文件 - 它失败并出现相同的异常。

>>> execfile("myscript.pyc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tix_email.pyc", line 1
SyntaxError: Non-ASCII character '\xd1' in file tix_email.pyc on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

针对 .pyc 或 .pyo 运行 profile/cProfile 的用例可能从未被解决。我还没有找到关于为什么会出现这种情况的规范解释,但由于 cPython 解释器的工作方式,通常预计主脚本不会被字节编译。解释器不会自动对主脚本进行字节编译,但会对导入的模块进行字节编译。这是关于该主题的一个问题: 为什么Python 会编译模块但不编译正在运行的脚本吗?

要解决您的问题,您可以启动脚本来调用您通常在要分析的 .pyo 中作为 main 执行的内容。公开的代码将非常简单。

launch.py​​

import foo
foo.call_my_func()

然后运行:

python -m cProfile -o ./temp/PROFILE.log 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 ):

This function takes a single argument
that can be passed to the exec
statement

execfile() is unable to execute *.pyc and *.pyo files - it fails with the same exception.

>>> execfile("myscript.pyc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tix_email.pyc", line 1
SyntaxError: Non-ASCII character '\xd1' in file tix_email.pyc on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

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

import foo
foo.call_my_func()

And then run:

python -m cProfile -o ./temp/PROFILE.log launch.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文