Loading Source Code With Inspect.getsource() Fails Saying It Can't Read A Built-in Class. (it's Not)
When I load the source code of a class from a module directly, it's fine: import arg_master inspect.getsource(func) When I load a module with spec_from_file_location and go for a
Solution 1:
I get the same error with your code, which oddly enough works for functions, but not classes. This below is essentially the same as your solution, just slightly less "hacky" as you don't have to mess with file paths:
import inspect, importlib
cls = getattr(importlib.import_module('arg_master'), 'ArgMaster')
print(inspect.getsource(cls))
This will work if the two modules in the same dir. If you need to do relative imports, might have to mess around with package=__pckage__
or similar
Post a Comment for "Loading Source Code With Inspect.getsource() Fails Saying It Can't Read A Built-in Class. (it's Not)"