How to debug python code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sushant.sirsikar@gmail.com

    How to debug python code?

    hi,
    I am new to Python programming.I am not getting exactly pdb.Can
    anyone tell me effective way to debug python code?
    Please give me any example.
    Looking for responce.
    Thank You.
    Sushant

  • ravi.annaswamy@gmail.com

    #2
    Re: How to debug python code?

    Here is an outstandingly well written article, with examples, from
    Stephen Ferg:



    Ravi

    Comment

    • bruno at modulix

      #3
      Re: How to debug python code?

      sushant.sirsika r@gmail.com wrote:[color=blue]
      > hi,
      > I am new to Python programming.I am not getting exactly pdb.Can
      > anyone tell me effective way to debug python code?[/color]

      (automated) unit tests + print statements + the interactive shell are
      usually enough. I almost never used pdb in 5+ years of Python programming.

      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Ravi Teja

        #4
        Re: How to debug python code?

        1.) Print statements
        2.) IDEs

        Most Python IDEs provide visual debuggers so that you don't have to use
        command line ones such as pdb.

        As with all languages that allow to be executated as a script as well,
        print statements usually get the job done quite well in most cases.

        Please read the Python FAQ (as you should for any new language you
        learn) in its entirety.
        The official home of the Python Programming Language

        It answers your IDE question you posted in the other thread as well as
        many of your future questions quite well.

        Comment

        • Kent Johnson

          #5
          Re: How to debug python code?

          sushant.sirsika r@gmail.com wrote:[color=blue]
          > hi,
          > I am new to Python programming.I am not getting exactly pdb.Can
          > anyone tell me effective way to debug python code?[/color]

          I too rely mostly on unit tests and print statements for debugging, but
          occasionally I use winpdb which is a pretty nice GUI debugger.


          Kent

          Comment

          • Thomas Guettler

            #6
            Re: How to debug python code?

            Am Thu, 30 Mar 2006 21:18:50 -0800 schrieb sushant.sirsika r:[color=blue]
            > hi,
            > I am new to Python programming.I am not getting exactly pdb.Can
            > anyone tell me effective way to debug python code?[/color]

            Hi,

            I try to debug the code while I type: Use "assert".

            Then if you get an AssertionError you can insert print statements or
            raise("var=%s var2=%s" % (var, var2))
            to narrow down the problem. I never used pdb.

            HTH,
            Thomas

            --
            Thomas Güttler, http://www.thomas-guettler.de/
            E-Mail: guettli (*) thomas-guettler + de
            Spam Catcher: niemand.leerman n@thomas-guettler.de

            Comment

            • Grant Edwards

              #7
              Re: How to debug python code?

              On 2006-03-31, sushant.sirsika r@gmail.com <sushant.sirsik ar@gmail.com> wrote:
              [color=blue]
              > I am new to Python programming.I am not getting exactly pdb.Can
              > anyone tell me effective way to debug python code?[/color]

              1) Read your code. Think.

              2) Add some "print" statements.

              3) goto 1)

              --
              Grant Edwards grante Yow! Where's th' DAFFY
              at DUCK EXHIBIT??
              visi.com

              Comment

              • R. Bernstein

                #8
                Re: How to debug python code?

                sushant.sirsika r@gmail.com writes:
                [color=blue]
                > hi,
                > I am new to Python programming.I am not getting exactly pdb.Can
                > anyone tell me effective way to debug python code?
                > Please give me any example.
                > Looking for responce.
                > Thank You.
                > Sushant[/color]

                Well, I guess (in addition to the other good suggestions in this
                thread) this is an obvious place to plug pydb


                If you are using pdb, I think you'll find pydb, um, better.

                Comment

                • R. Bernstein

                  #9
                  Re: How to debug python code?

                  Dennis Lee Bieber <wlfraed@ix.net com.com> writes:
                  [color=blue]
                  > On 30 Mar 2006 21:18:50 -0800, sushant.sirsika r@gmail.com declaimed the
                  > following in comp.lang.pytho n:
                  >[color=green]
                  > > hi,
                  > > I am new to Python programming.I am not getting exactly pdb.Can
                  > > anyone tell me effective way to debug python code?[/color]
                  >
                  > I think I toyed with pdb back around 1993... Never needed it...
                  >
                  > Of course, with so many different languages and debuggers in my
                  > life, I've never found time to master any but the old VMS debugger
                  > (which is nothing more than a very complex error handler <G>)[/color]

                  That's one reason why in my bash and GNU make debugger (and in
                  extending pdb), I've stuck to the gdb command set: the effort that is
                  spent mastering gdb can be transfered in the GNU Make, Bash, *AND*
                  Python debuggers.

                  Comment

                  • pruebauno@latinmail.com

                    #10
                    Re: How to debug python code?

                    sushant.sirsika r@gmail.com wrote:[color=blue]
                    > hi,
                    > I am new to Python programming.I am not getting exactly pdb.Can
                    > anyone tell me effective way to debug python code?
                    > Please give me any example.
                    > Looking for responce.
                    > Thank You.
                    > Sushant[/color]

                    If you are having issues you also might want to try:

                    and see if it finds your problem. You might get lucky.

                    Or just make the bigger install:

                    that one already includes pychecker and a python debugger.

                    Sorry for the noise if you were just looking for instructions for pdb.

                    Comment

                    • Johannes Nix

                      #11
                      Re: How to debug python code?

                      Dennis Lee Bieber <wlfraed@ix.net com.com> writes:
                      [color=blue]
                      > My typical debugging technique is the infamous "wolf fence" (there's
                      > one wolf in Alaska, how do you find it? First build a fence down the
                      > middle of the state, wait for the wolf to howl, determine which side of
                      > the fence it is on. Repeat process on that side only, until you get to
                      > the point where you can see the wolf). In other words; put in a few
                      > "print" statements until you find the statement that is failing (then
                      > maybe work backwords from the "tracks" to find out where the wolf/bug
                      > comes from).[/color]


                      I think the advantage of this method is that one has to become
                      clear what the state of the program should look like.

                      For the same reason, unit tests may help to develop a
                      proper specification of the task of a module.

                      However, pdb's port mortem debugging can be helpful when a
                      program takes a lot of time to hit the error, and
                      a large amount of data is in play, for example for
                      numerical computations.

                      Johannes

                      Comment

                      • james.wondrasek@gmail.com

                        #12
                        Re: How to debug python code?

                        I don't have an example, but what I do is insert:

                        import pdb
                        pdb.set_trace()

                        before the code that I think is causing the problem
                        (or that the traceback barfed at).

                        Then hit 'l' (lowercase L) to get a list of the code
                        which is being executed.

                        Use 'p' to look at variables. eg p some_local

                        Use 'n' to execute the current line of code
                        (doesn't follow method or function calls)

                        Use 's' to step through code, including following
                        function and method calls.

                        Finally, 'c' to continue execution as normal.

                        There are other options, but that should get you started.
                        And look at the article linked elsewhere in this thread.

                        pdb - because fencing off Alaska is too damn slow, binary
                        search or not.

                        jtm

                        Comment

                        Working...