Suggestion for improved ImportError message

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kent Tenney

    Suggestion for improved ImportError message

    Howdy,

    I was just bit by

    from image import annotate

    ImportError: cannot import name annotate

    I found the problem via

    import image
    print image.__file__

    which made it clear that the wrong image module had been found.

    It would be nice if ImportError announced this up front.

    cannot import name annotate from /usr/<snip>/image.pyc

    Thanks,
    Kent

  • Wojtek Walczak

    #2
    Re: Suggestion for improved ImportError message

    Dnia Wed, 13 Aug 2008 20:55:38 +0000 (UTC), Kent Tenney napisa³(a):
    from image import annotate
    >
    ImportError: cannot import name annotate
    >
    I found the problem via
    >
    import image
    print image.__file__
    >
    which made it clear that the wrong image module had been found.
    >
    It would be nice if ImportError announced this up front.
    Then go for it :-) You can prepare a patch and ask on python-dev
    if the developers are interested.

    I was never hacking the import things on C level before,
    but a hint: you have to modify import_from function from
    Python/ceval.c

    My quick attempt:



    --
    Regards,
    Wojtek Walczak,

    Comment

    • Wojtek Walczak

      #3
      Re: Suggestion for improved ImportError message

      Dnia Wed, 13 Aug 2008 22:15:48 +0000 (UTC), Wojtek Walczak napisa³(a):
      Then go for it :-) You can prepare a patch and ask on python-dev
      if the developers are interested.
      >
      I was never hacking the import things on C level before,
      but a hint: you have to modify import_from function from
      Python/ceval.c
      >
      My quick attempt:
      http://www.stud.umk.pl/~wojtekwa/pat...-py2.5.1.patch
      Uh, and an example:

      Python 2.5.1 (r251:54863, Aug 14 2008, 00:04:00)
      [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.
      >>from os import qweasd
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ImportError: cannot import name qweasd (/home/gminick/Python-2.5.1/Lib/os.pyc)
      >>>
      --
      Regards,
      Wojtek Walczak,

      Comment

      • Kent Tenney

        #4
        Re: Suggestion for improved ImportError message

        Then go for it You can prepare a patch and ask on python-dev
        if the developers are interested.
        hehe, I'll get a C level patch accepted right after I
        out-swim Mike Phelps.

        I was never hacking the import things on C level before,
        but a hint: you have to modify import_from function from
        Python/ceval.c
        Am I correct in thinking that PyPy would mean low level
        stuff like this will be Python instead of C?
        That would be nice.

        My quick attempt:
        Quick indeed!
        Very cool.

        Thanks,
        Kent





        Comment

        • Wojtek Walczak

          #5
          Re: Suggestion for improved ImportError message

          On Thu, 14 Aug 2008 01:35:44 +0000 (UTC), Kent Tenney wrote:
          Then go for it You can prepare a patch and ask on python-dev
          if the developers are interested.
          >
          hehe, I'll get a C level patch accepted right after I
          out-swim Mike Phelps.
          It's really not that hard. The only hard thing (harder than
          writing the code) might be to win the acceptance of the core
          developers that this change is really needed ;-)

          I was never hacking the import things on C level before,
          but a hint: you have to modify import_from function from
          Python/ceval.c
          >
          Am I correct in thinking that PyPy would mean low level
          stuff like this will be Python instead of C?
          That would be nice.
          I don't know PyPy, but I guess you're right here.


          --
          Regards,
          Wojtek Walczak,

          Comment

          • Lie

            #6
            Re: Suggestion for improved ImportError message

            On Aug 14, 5:10 pm, Wojtek Walczak
            <gmin...@nie.ma .takiego.adresu .w.sieci.plwrot e:
            On Thu, 14 Aug 2008 01:35:44 +0000 (UTC), Kent Tenney wrote:
            Then go for it  You can prepare a patch and ask on python-dev
            if the developers are interested.
            >
            hehe, I'll get a C level patch accepted right after I
            out-swim Mike Phelps.
            >
            It's really not that hard. The only hard thing (harder than
            writing the code) might be to win the acceptance of the core
            developers that this change is really needed ;-)
            I think the acceptance for this wouldn't be that hard since there is
            no real issue for regression (the only one I could think of is for
            doctest module, although I'm not sure there are any reason to test for
            failed import in doctest)
            I was never hacking the import things on C level before,
            but a hint: you have to modify import_from function from
            Python/ceval.c
            >
            Am I correct in thinking that PyPy would mean low level
            stuff like this will be Python instead of C?
            That would be nice.
            >
            I don't know PyPy, but I guess you're right here.
            >
            --
            Regards,
            Wojtek Walczak,http://www.stud.umk.pl/~wojtekwa/

            Comment

            • bearophileHUGS@lycos.com

              #7
              Re: Suggestion for improved ImportError message

              Lie:
              >I'm not sure there are any reason to test for failed import in doctest)<
              I have code that uses numpy if available, otherwise uses slower normal
              Python code. Inside the doctests I'd like to test both situations...

              Bye,
              bearophile

              Comment

              • Benjamin

                #8
                Re: Suggestion for improved ImportError message

                On Aug 13, 3:55 pm, Kent Tenney <kten...@gmail. comwrote:
                Howdy,
                >
                I was just bit by
                >
                from image import annotate
                >
                ImportError: cannot import name annotate
                >
                I found the problem via
                >
                import image
                print image.__file__
                >
                which made it clear that the wrong image module had been found.
                >
                It would be nice if ImportError announced this up front.
                Why? It says the file where the error originated in the traceback.
                >
                cannot import name annotate from /usr/<snip>/image.pyc
                >
                Thanks,
                Kent

                Comment

                • Lie

                  #9
                  Re: Suggestion for improved ImportError message

                  On Aug 15, 7:42 pm, bearophileH...@ lycos.com wrote:
                  Lie:
                  >
                  I'm not sure there are any reason to test for failed import in doctest)<
                  >
                  I have code that uses numpy if available, otherwise uses slower normal
                  Python code. Inside the doctests I'd like to test both situations...
                  Why? Is there a difference in result if you have used numpy and python
                  code? If that is, I smell a bad code. What numpy and python code
                  version would return in that situation should be the same (practically
                  it isn't always feasible though).
                  Bye,
                  bearophile

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: Suggestion for improved ImportError message

                    Lie wrote:
                    >I have code that uses numpy if available, otherwise uses slower normal
                    >Python code. Inside the doctests I'd like to test both situations...
                    >
                    Why? Is there a difference in result if you have used numpy and python
                    code? If that is, I smell a bad code. What numpy and python code
                    version would return in that situation should be the same (practically
                    it isn't always feasible though).
                    maybe the point is to use doctest to *verify* that the code generates
                    the same result whether or not numpy is used?

                    </F>

                    Comment

                    Working...