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