r/PythonLearning • u/RomfordNavy • 28d ago
Import hook between AST and compile
I am trying to create an Import Hook inserted after the source code has been parsed into AST but before it is compiled. Looking for a way to keep using most of the existing Python import machinery but modify the source code before it is compiled.
So far I have the following code but how do I get access to the source code from the module in custom_import?
import builtins
from inspect import getmembers
base_imp = builtins.__import__
def custom_import(*args, **kwargs):
module = base_imp(*args, **kwargs)
for name, val in getmembers(module):
print("in custom_import.", name, val)
return module
builtins.__import__ = custom_import
print("in Main.py before import")
import Module1
print("in Main.py after import")import builtins2
u/Sea-Ad7805 28d ago
Maybe have a look at this example.
It demonstrates the custom import hook, including parsing source code into an AST, injecting a statement, compiling the modified AST, and executing the imported module.
1
u/RomfordNavy 28d ago
Interesting, however although code runs fine in Memory Graph Web Debugger when copied and run in Python 3.15 it doesn't run the CustomLoader.
2
u/aroberge 28d ago edited 28d ago
As I mentioned to you on the python-discuss site (https://discuss.python.org/t/modify-import-command/109468), you need to use an import hook. I mentioned to you my package ideas; the code has since been somewhat refactored making it easier to see what needs to be done. For example, on python-discuss you mentioned wanting to change the code before it is compiled into bytecode. This is the step shown at https://github.com/aroberge/ideas/blob/master/src/ideas/loader.py#L106
The code you have is definitely not an import hook: it will only work on files where you explicitly write an import using the equivalent of
__import__. A file imported from that first file will NOT use you code. A proper import hook needs to be inserted into sys.meta_path and is something that is used when a normalimportkeyword is used.That being said, like experienced Python programmers (I'm not one of them) have told you on Python-discuss, trying to eliminate some code and short-circuit some constant import will have a completely negligible performance impact.