Python Forum
import module with syntax error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import module with syntax error
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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)
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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?
buran likes this post
Reply
#5
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Yes, but that's a subclass. So catching SyntaxError will catch IndentationError as well.
buran likes this post
Reply
#7
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
ah... since import runs the code, too, that does open up any exception.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import a module for use in type hint? Milosz 0 153 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 398 Oct-27-2021, 03:32 AM
Last Post: Tek
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 351 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 477 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Line 42 syntax error..Help!!1 patpython 4 596 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  Unexplained Invalid syntax Error cybertooth 5 661 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  I have a syntax error! rohes_kaugummi 3 797 Jun-23-2021, 05:24 PM
Last Post: deanhystad
Heart near "(": syntax error Maryan 3 1,118 Jun-21-2021, 08:05 AM
Last Post: ibreeden
Exclamation getting a syntax error and i don't know why ilikedofs 10 1,527 May-13-2021, 08:52 PM
Last Post: deanhystad
  Even number code syntax error MrCeez 1 786 May-02-2021, 06:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020