$30 off During Our Annual Pro Sale. View Details »

Goodbye Print, Hello Debugger - Nina Zakharenko DjangoCon 2019

Goodbye Print, Hello Debugger - Nina Zakharenko DjangoCon 2019

This talk was given on Tuesday September 24th 2019 at DjangoCon US in San Diego.

Still debugging your code by using print? Learn how to level up your ability to troubleshoot complex code situations by using the power of a fully-featured debugger in this talk aimed at all levels of programming ability.

Debuggers allow you to examine your program state, watch as the values of important variables change, and even modify the content of variables on the fly. Once I gave up using print to debug and troubleshoot, my productivity as a programmer increased, and yours can too!

I’ll showcase the variety of debugger tools available - from pdb, the simplest command line debugger that’s part of the standard library, to fancy graphical debuggers available in Python IDEs. Join me as we walk through real code together using debugger tools in a hands-on way to help us diagnose problems and bugs. The skills you’ll learn in this talk will allow you to quickly use these tools in your own code bases for fun, school, or work.

https://2019.djangocon.us/talks/goodbye-print-hello-debugger/

Nina Zakharenko

September 24, 2019
Tweet

More Decks by Nina Zakharenko

Other Decks in Technology

Transcript

  1. nina.to/djangocon2019
    ---
    NINA ZAKHARENKO
    ---
    GOODBYE PRINT,
    HELLO DEBUGGER
    ---
    @NNJA

    View Slide

  2. LIVETWEET
    #DJANGOCON
    @NNJA
    nina.to/djangocon2019

    View Slide

  3. whoami?
    @nnja
    nnja

    nina.to
    nina.to/djangocon2019

    View Slide

  4. WHY USE
    DEBUGGERS?
    @nnja • nina.to/
    djangocon2019

    View Slide

  5. DEMO APPLICATION
    @nnja • nina.to/djangocon2019

    View Slide

  6. CALL API & GET JSON
    def repos_with_most_stars(languages, min_stars, sort, order):
    query = create_query(languages, min_stars)
    parameters = {"q": query, "sort": sort, "order": order}
    response = session.get(github_api_url, params=parameters)
    if response.status_code != 200:
    raise GitHubApiException(response.status_code)
    response_json = response.json()
    items = response_json["items"]

    View Slide

  7. PRINT
    @nnja • nina.to/djangocon2019

    View Slide

  8. A DEBUGGER HELPS YOU
    EXAMINE THE STATE
    OF A RUNNING PROGRAM
    @nnja • nina.to/djangocon2019

    View Slide

  9. DEBUGGING
    @nnja • nina.to/djangocon2019

    View Slide

  10. WHAT YOU'LL LEARN TODAY
    ‣ why use debuggers?
    ‣ breakpoints, other debugger fundamentals
    ‣ my workflow
    ‣ tools: pdb, ipdb, IDEs
    ‣ breakpoint() in Python 3.7
    ‣ demos, tips & tricks, when to use what
    @nnja • nina.to/djangocon2019

    View Slide

  11. (THIS IS MY WAY)
    NOT ONE "RIGHT" WAY
    @nnja • nina.to/djangocon2019

    View Slide

  12. TYPES OF
    DEBUGGERS
    @nnja • nina.to/djangocon2019

    View Slide

  13. CLI
    ‣ pdb (in standard library)
    ‣ ‣ ipdb- ipdb (via pip install
    ipdb)
    ‣ & more...
    @nnja • nina.to/djangocon2019

    View Slide

  14. @nnja

    View Slide

  15. GRAPHICAL DEBUGGERS
    ‣ pudb (graphical CLI)
    ‣ IDEs
    ‣‣ Visual Studio Code
    ‣PyCharm
    ‣ & others ...
    @nnja • nina.to/djangocon2019

    View Slide

  16. View Slide

  17. BREAKPOINTS
    @nnja • nina.to/djangocon2019

    View Slide

  18. PYTHON 3.7?
    USE breakpoint()
    @nnja • nina.to/djangocon2019

    View Slide

  19. breakpoint() ADVANTAGES
    ‣ set your debugger of choice
    (I like ipdb)
    ‣export PYTHONBREAKPOINT=ipdb.set_trace
    (only in Python >= 3.7)
    @nnja • nina.to/djangocon2019

    View Slide

  20. breakpoint() ADVANTAGES
    environment variable that
    allows you skip all debug
    breakpoints
    $ PYTHONBREAKPOINT=0 python3.7 prod_code.py
    (only in Python >= 3.7)
    @nnja • nina.to/djangocon2019

    View Slide

  21. CLI BREAKPOINTS IN PYTHON < 3.7
    ‣ #1 Add them directly to your code
    import pdb; pdb.set_trace()
    # or
    import ipdb; ipdb.set_trace()
    ‣ #2: Set them interactively
    $ python -m pdb hello.py
    (Pdb) break 7
    Breakpoint 1 at hello.py:7
    @nnja • nina.to/djangocon2019

    View Slide

  22. DEBUGGER
    FUNDAMENTALS
    @nnja • nina.to/djangocon2019

    View Slide

  23. 5 MOST IMPORTANT COMMANDS
    ‣ l(ist) or ll for long list
    ‣ n(ext) - next line
    ‣ s(tep) - into
    ‣ c(ontinue) - until next breakpoint
    ‣ h(elp)
    @nnja • nina.to/djangocon2019

    View Slide

  24. @nnja • nina.to/djangocon2019

    View Slide

  25. DEBUGGERS
    IN IDES
    @nnja • nina.to/djangocon2019

    View Slide

  26. PYTHON EXTENSION FOR VS CODE
    ‣ 1. Download VS Code
    ‣ 2. Install Python Extension
    ‣ 3. Create a Debugger Configuration

    View Slide

  27. VISUAL BREAKPOINTS
    @nnja • nina.to/djangocon2019

    View Slide

  28. WHEN TO
    USE WHAT?
    @nnja • nina.to/djangocon2019

    View Slide

  29. WHEN TO USE WHAT?
    CLI DEBUGGING
    ‣ small programs and scripts
    ‣ want to work from command line
    IDE DEBUGGING
    ‣ complex code or large codebase
    ‣ debug templates
    @nnja • nina.to/djangocon2019

    View Slide

  30. TIPS & TRICKS
    @nnja • nina.to/djangocon2019

    View Slide

  31. TRICK: IPYTHON INTERACTIVE MODE ALIAS IN .PDBRC
    import IPython
    from traitlets.config import get_config
    cfg = get_config()
    # enable syntax highlighting
    cfg.InteractiveShellEmbed.colors = "Linux"
    cfg.InteractiveShellEmbed.confirm_exit = False
    alias interacti IPython.embed(config=cfg)
    download here: nina.to/pdbrc

    View Slide


  32. NEW!
    DEBUGGING
    JUPYTER NOTEBOOKS
    IN VS CODE
    nina.to/debug_jupyter
    @nnja • nina.to/djangocon2019

    View Slide

  33. !
    GOTCHAS
    @nnja • nina.to/djangocon2019

    View Slide

  34. !
    DON'T LEAVE BREAKPOINTS
    IN PRODUCTION CODE!
    @nnja • nina.to/djangocon2019

    View Slide

  35. !
    TIP: USE
    git pre-commit HOOKS
    ‣Prevent commits that
    match conditions
    ‣I use pre-commit.com
    ‣includes debug-statements
    hook
    ‣checks for:
    pdb, ipdb, pudb,
    breakpoint() & more
    @nnja

    View Slide

  36. !
    WRAPPING UP
    @nnja • nina.to/djangocon2019

    View Slide

  37. FIXING BUGS IS A PROCESS OF CONFIRMING,
    ONE BY ONE,
    THAT THE THINGS YOU BELIEVE TO BE TRUE
    ABOUT THE CODE ARE TRUE.
    WHEN YOU FIND AN ASSUMPTION THAT ISN'T,
    YOU FOUND A CLUE.
    (Paraphrased from the art of debugging with GBS, DDD, and Eclipse)
    @nnja • nina.to/djangocon2019

    View Slide

  38. ADDITIONAL RESOURCES
    ‣ Interactive pdb Tutorial (also in Korean)
    ‣ Python 3.7’s new builtin breakpoint — a
    quick tour
    THANKS

    !
    Thea Flowers
    @nnja • nina.to/djangocon2019

    View Slide

  39. THANKS!
    @NNJA
    SLIDES:
    nina.to/djangocon2019
    PYTHON @ MICROSOFT:
    aka.ms/djangocon2019

    View Slide