Can one override "using" or "import" keywords?

Is it possible to change the behavior of a particular import or using command? I know this sounds pretty terrible, and I’m only 10% serious about actually doing this. In particular, can one modify import SomePackage to trigger some function even when the package is already loaded?

This is in the context of an issue with PyPlot + PackageCompiler I’m trying to fix. If PyPlot is in a sysimage, its init seems to run before any other Julia code runs, so it cannot hook up to IJulia cell triggers or find the appropriate backend. To do environment discovery correctly, one could rewrite PyPlot to set up the backend and IJulia hooks lazily when one runs the first plot command, but that seems like a lot of work. Could one override import PyPlot to run a function that would hook PyPlot to IJulia if it’s not already hooked up?

I’m a bit embarassed about this, but I’m having a bit of trouble even finding import in the Julia source. The docs point to the location of the keywords in the documentation, searching the repository for “import” is not very fruitful, and @which seems to tell me that import is not a function which takes a symbol. So maybe if someone could just point me to where they are, I can convince myself this can’t/shouldn’t be done.

I don’t think “import” or “using” are really function calls. I believe they are used to tell the compiler “Bring this module/methods into my namespace.” So no real code is actually generated it just increases the methods the compiler “knows” about.

It might be nice if the compiler allowed you to install “hooks” that are called when certain events occur (like import or using) but I haven’t seen anyone talk about something like that.

import or using is not an event. However, loading of a module is. I believe there is a callback for loading a new module.