开发者

Python cProfile: Is it possible for pyo file

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

SyntaxErro开发者_运维问答r: 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜