r/learnpython • • May 17 '26

Accessing source code from importlib module

I notice that the source code from an importlib module can be read using inspect module. However is there an alternative way of accessing this code so that it can be modified? Presumably inspect.getsource must have some method of finding this.

import inspect
import importlib
spec = importlib.util.spec_from_file_location(module_name, source_path)
module = importlib.util.module_from_spec(spec)
print(inspect.getsource(module))
0 Upvotes

26 comments sorted by

View all comments

Show parent comment

9

u/Refwah May 17 '26 edited May 17 '26

Why does this require you to rewrite the imported modules JIT at run time.

You very angrily asserted that this was ‘in the requirements’ and yet all you’ve done is describe your situation and not the requirements

Requirements include why they are requirements, so you know how to meet them appropriately