Reputation: 7098
In a debugger I would like to verify that the source path matches the code that is running. One way to do this in Python 3 is to look at the module byte-compiled file (a .pyc
or .pyo
) which has a field for the source size modulo 2**32
.
So now the question is given a frame object how to I find the associated byte-compiled module path. (Again a .pyc
or .pyo
file).
print(inspect.getfile(inspect.currentframe()))
will get me a source-file name that I can try to convert to the bytecode module name, but nicer would be if there were a more reliable way since clearly Python had to read the module in the first place.
What's the most reliable way?
Upvotes: 0
Views: 141
Reputation: 1123260
Look at the globals (it's the f_globals
attribute of a frame); it has a wealth of info:
__name__
- the module name__file__
- the module filename__cached__
- the .pyc
bytecode cache filenameUpvotes: 2