Actually, please feel free to contribute help, but I managed to create a working example of an import hook.
self.forbidden = list()
def find_module(self, module_name, package_path):
print("Searching the path... Return.")
if module_name in self.forbidden:
def load_module(self, module_name):
The problem occurred when I executed the program from the Python IDLE, but when I executed the program from the command line it worked.
[<__main__.Restricted object at 0x0000000003C6A9E8>]
Command line output:
[<__main__.Restricted object at 0x0000000002F69400>]
Traceback <most recent call last>"
File "C:\Users\lrh9\Documents\Code\Personal Code\test.py", line 23, in <module>
File "C:\Users\lrh9\Documents\Code\Personal Code\test.py", line 15, in load_module
My first guess about what happened would be that the IDLE imports random when it starts. It probably has the random module all ready cached. If a module is cached, importing it will merely result in the cached module's finder being used and/or the cached module being returned. This would skip the new hook you've inserted.
I bet that if I had reloaded the random module, it would have worked in the IDLE.