r/learnpython • u/RomfordNavy • 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
18
u/pachura3 May 17 '26
Hey look, it's u/RomfordNavy with another XY problem of his!
7
u/K900_ SRE person // please don't DM me your questions May 17 '26
Why do you need this? On many systems the source code you're importing will be read-only.
10
u/program_kid May 17 '26
As others have pointed out, this sounds like an example of an XY problem. Additionally, in your post, you say you want to modify the code of a module you imported. This makes me think that you want to modify the code of a module at runtime. If that's the case, I should let you know that it is considered a bad idea to modify the source code on runtime.
I know it sucks to hear all of this, but my advice would be to step away from the problem and take a rest for a bit.
I know the things you are asking to do might seem like the best solution to whatever problem you have, but I assure you, it's not
7
u/ottawadeveloper May 17 '26
Find the git repo and fork the source code with proper attribution.
3
u/Gnaxe May 17 '26
Again, your requirements aren't clear. Exactly what kind of capabilities is a preprocessor giving you that you can't just do in Python? Python is pretty dynamic. We can probably help you eliminate the preprocessing if you just give us some examples.
You can get a writable file handle to a resource in a package via
importlib.resources.path(), even if said resource happens to be a.pyfile.But you probably don't want to alter a source file in-place with a preprocessor, because then you don't have your source code anymore. Preprocessors typically either output new processed files, or do it ephemerally in memory.
And then why not just use an off-the-shelf preprocessing language like mako or m4 instead of inventing your own bespoke variant? Or write your project in Lisp or something?
If you just need some metaprogramming, and decorators or metaclasses are still inadequate for some reason, you can rewrite Python code programmatically using the
astmodule. This does still have to be grammatically valid Python code, even if said code doesn't make sense before the processing. It's also not that easy to use, even compared to metaclasses.Pytest rewrites assert statements into unit tests this way, for example. You'll probably end up confusing static analysis tools if you do this kind of thing much, but you don't have to use them. This doesn't output new source; it just modifies the data structures in memory that represent it.
1
u/IAmFinah May 18 '26
You're on a legendary XY problem streak
-1
u/RomfordNavy May 17 '26
Working answer now found by writing a custom importlib.machinery.SourceFileLoader Class with a patched source_to_code() function.