Python Forum
import module with syntax error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: import module with syntax error (/thread-34052.html)



import module with syntax error - Skaperen - Jun-21-2021

i'm importing a module that may have errors in the source code. i tried to catch it like:
try:
    import flaky
except ImportError:
    print('bad module')
but it gave me SyntaxError instead. ImportError didn't cover it. how can i find all the exceptions i might need to cover?


RE: import module with syntax error - bowlofred - Jun-21-2021

The imported file will be compiled (so may raise SyntaxError) and will be executed. Good modules will try to limit what is executed (putting most things in __init()__ or methods rather than in the main script). But that means that theoretically the code could raise any error.

If the only thing you're worried about is bad code/SyntaxError, then just catch that. I'm struggling to think of similar errors that would be raised by the compiler/interpreter rather than the code within (but maybe someone else will know of other situations).

If you're going to use the exception to fail with a nice message rather than continue, then catching Exception and reporting it seems reasonable.

import sys
try:
    import flakey
except Exception as e:
    print("Failed to complete import.  Cancelled.")
    print(type(e), e)
    sys.exit(1)



RE: import module with syntax error - Skaperen - Jun-22-2021

i'm wondering what else there is besides SyntaxError. i've seen some other errors in the code before. i am wanting to know what are all the possible exceptions that could be raised.


RE: import module with syntax error - bowlofred - Jun-22-2021

Technically any exception can be raised on an import (including those that are in the standard library as well as custom exceptions). But if you look at the built-in Exceptions, the only two errors that call out being raised on import are ImportError and SyntaxError.

What is your intent with catching the SyntaxError? What do you want the program to do when the import fails?


RE: import module with syntax error - buran - Jun-22-2021

(Jun-22-2021, 03:42 AM)bowlofred Wrote: But if you look at the built-in Exceptions, the only two errors that call out being raised on import are ImportError and SyntaxError.

Another one that comes to my mind is IndentationError.


RE: import module with syntax error - bowlofred - Jun-22-2021

Yes, but that's a subclass. So catching SyntaxError will catch IndentationError as well.


RE: import module with syntax error - Skaperen - Jun-22-2021

(Jun-22-2021, 03:42 AM)bowlofred Wrote: What is your intent with catching the SyntaxError? What do you want the program to do when the import fails?
that is mostly undecided. it will initially be some error message to the user. that's how i plan to initially test it.


RE: import module with syntax error - Skaperen - Jun-22-2021

ah... since import runs the code, too, that does open up any exception.