Skip to content

import error is not reported #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
den-run-ai opened this issue Nov 2, 2015 · 6 comments
Closed

import error is not reported #111

den-run-ai opened this issue Nov 2, 2015 · 6 comments

Comments

@den-run-ai
Copy link
Contributor

this piece of code in importhook.cs just cleans up the import error without passing it to pythonnet:

                // There was an error
                if (!Exceptions.ExceptionMatches(Exceptions.ImportError)) {
                    // and it was NOT an ImportError; bail out here.
                    return IntPtr.Zero;
                }
                // Otherwise,  just clear the it.
                Exceptions.Clear();
@rmadsen-ks
Copy link
Contributor

Clearing the ImportErrors a big problem for me because it changes the way python handles a missing module. Normally ImportErrors would be propagated up through the chain of imports. This means that I am not able to fall back to an alternative module if import raised an ImportError. Instead I have to check if the contents of the module is what I expect after loading it.

When import fails(with an ImportError), it still returns a module object, but its empty.

In the application I'm working on we allow users to define plugins by creating a module and putting it in plugins folder. We then scan the folder for plugins on runtime to determine located installed plugins. Since all ImportErrors (from regular python plugins) does not appear, I cannot give the user a proper error message when import failed. Instead I have to just write "no plugins found" when the issue was actually a mistyped import.

Maybe the exception should be saved and re-added later if it was also not a CLR module?

@den-run-ai
Copy link
Contributor Author

@rmadsen-ks see a pending pull request for this:

#298

@rmadsen-ks
Copy link
Contributor

@denfromufa, my issue is not that the error is not detailed enough, but that ImportErrors are ignored. Since ImportHook is overridden by pythonnet, this changes the way all python modules are imported not just CLR ones.

@rmadsen-ks
Copy link
Contributor

@denfromufa, it seemed the issue was caused by me having a DLL with the same name as a python module. In normal cases, the ImportError would be emitted, but not if there is an already loaded assembly with the same name.

It seems a bit probematic because there is a good chance that someone would write a wrapper DLL with the same name as a python dll to call into python with. Not sure if different issue though.

@filmor
Copy link
Member

filmor commented Apr 5, 2017

I also had problems with this hook. I think it the long run we should replace it by an entry to sys.meta_path, that should stop it from interfering with the import mechanism.

@filmor
Copy link
Member

filmor commented Aug 6, 2021

Since the import hook is gone now, this should be fixed.

@filmor filmor closed this as completed Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants