[IronPython] Importing embedded .py files

JoeSox joesox at gmail.com
Tue Jul 4 01:01:04 PDT 2006


On 7/4/06, Nathan R. Ernst <ernsnat at iit.edu> wrote:
> Out of curiosity, is there any sort of hook into the engine that would allow
> you search for a module on your own?  e.g. a search of sys.path did not find
> the specified module, so before raising an import error, we give a set of
> registered event handlers a crack at it? I would envision a cancelable event
> would be raised, such that the first handler (if any) to successfully find
> the module would prevent further handlers from being called.
>
> I would need to take a closer look at the engine interfaces to see if this
> would work, but I'd think this would be a relatively minor change (if its
> not there already, of course).

If I understand you correctly, I could just catch the "could not
import" exception (because that is really the only "hook" into the
engine in this case, and then grab the module name from the exception
string then pass it to something like:

public searchForModule(string modname)
{
           Assembly myAssembly = Assembly.GetExecutingAssembly();
            string resName = String.Empty;
            foreach (string resourceName in
myAssembly.GetManifestResourceNames())
            {
                if (resourceName.IndexOf(modname+".py") >= 0)
                {
                    do something;
                    break;
                }
            }
}
-- 
Later, Joe


More information about the users mailing list