Better error message on recursive import

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Guettler

    #1

    Better error message on recursive import

    Hi,

    why does Python only raise ImportError if it fails caused by a recursive import?

    I know what's wrong. But I guess many beginner don't know what's wrong. I don't
    want much, just "RecursiveImpor tError" instead of "ImportErro r". Is this possible?

    Thomas


    --
    Thomas Guettler, http://www.thomas-guettler.de/
    E-Mail: guettli (*) thomas-guettler + de
  • Marc 'BlackJack' Rintsch

    #2
    Re: Better error message on recursive import

    On Thu, 11 Sep 2008 12:43:34 +0200, Thomas Guettler wrote:
    why does Python only raise ImportError if it fails caused by a recursive
    import?
    >
    I know what's wrong. But I guess many beginner don't know what's wrong.
    I don't want much, just "RecursiveImpor tError" instead of "ImportErro r".
    Is this possible?
    Can you give an example of such a recursive import you want the special
    exception be raised?

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Thomas Guettler

      #3
      Re: Better error message on recursive import

      Hi,
      Can you give an example of such a recursive import you want the special
      exception be raised?
      ===cat one.py
      from two import testtwo
      def testone():
      print "one"

      ===cat two.py
      import one
      def testtwo():
      print "two"

      ===python one.py
      Traceback (most recent call last):
      File "one.py", line 1, in <module>
      from two import testtwo
      File "/mnt/home/tguettler/tmp/rec/two.py", line 1, in <module>
      import one
      File "/mnt/home/tguettler/tmp/rec/one.py", line 1, in <module>
      from two import testtwo
      ImportError: cannot import name testtwo


      --
      Thomas Guettler, http://www.thomas-guettler.de/
      E-Mail: guettli (*) thomas-guettler + de

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Better error message on recursive import

        On Fri, 12 Sep 2008 09:47:42 +0200, Thomas Guettler wrote:
        >Can you give an example of such a recursive import you want the special
        >exception be raised?
        >
        ===cat one.py
        from two import testtwo
        def testone():
        print "one"
        >
        ===cat two.py
        import one
        def testtwo():
        print "two"
        >
        ===python one.py
        Traceback (most recent call last):
        File "one.py", line 1, in <module>
        from two import testtwo
        File "/mnt/home/tguettler/tmp/rec/two.py", line 1, in <module>
        import one
        File "/mnt/home/tguettler/tmp/rec/one.py", line 1, in <module>
        from two import testtwo
        ImportError: cannot import name testtwo
        This is an awkward situation anyway because here are *three* modules
        involved. You start `one.py` which will be imported as `__main__`.
        `__main__` imports `two` and `two` imports a *new* module `one`! Which
        tries to import `testtwo` from `two` which doesn't exist at that time.
        Even if you rearrange the code to load properly `one.py` is loaded and
        executed *twice* and you end up with two distinct modules generated from
        that file.

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Wojtek Walczak

          #5
          Re: Better error message on recursive import

          On Thu, 11 Sep 2008 12:43:34 +0200, Thomas Guettler wrote:

          Hello,
          why does Python only raise ImportError if it fails caused by a recursive import?
          >
          I know what's wrong. But I guess many beginner don't know what's wrong.
          I don't think that you're right here. I can't remember any beginner
          asking such a question on p.c.py. I think they would come and ask
          if it really was problematic.

          And, anyway, I don't know how to answer your question :-)

          --
          Regards,
          Wojtek Walczak,

          Comment

          Working...