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
  Syntax error for "root = Tk()" dlwaddel 15 1,160 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 373 Jan-19-2024, 01:20 PM
Last Post: rob101
  is import cointegration_analysis a recognized module mitcht33 1 425 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,392 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 715 Aug-06-2023, 01:09 AM
Last Post: aupres
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,663 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Syntax error while executing the Python code in Linux DivAsh 8 1,544 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,296 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,243 Jan-15-2023, 07:49 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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