Python comes with its own built-in debugger called `pdb`.

Let's talk about that in a mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
`pdb` stands for Python debugger. It is a built-in part of Python's standard library

Here is a link to the documentation:

docs.python.org/3/library/pdb.โ€ฆ
To start debugging code with `pdb`, you need to write some code.

Here's the code you'll be using:
There are several different ways to debug code with `pdb`. Open up the Python REPL / interpreter in the same folder as the code you wish to debug.

Then run the following to run your code in the Python debugger
`pdb` uses many of the same commands as `gdb`, the C++ debugger. If you are familiar with `gdb`, then using `pdb` will be a snap!

In this code, you call `run()` to run your code. Then you type `continue` to continue the run. You can also type `c` as a shortcut for `continue`
When debugging, your code will run until one of the following occurs:

๐Ÿ An exception is raised
๐Ÿ You hit a breakpoint
๐Ÿ The code finishes successfully
You can also run the Python debugger on the command line by using a command like this one:
The output of running the code on the command line is different than running it in the REPL

The prompt, (Pdb) will appear. You will need to issue a command to `continue` or to step through your code
Here is an example console session where you use the `step` or `s` command to step through the code with `pdb`
You can add a breakpoint inside the `pdb` session itself by typing `break` followed by the line number you wish to add the breakpoint to
You can also add a breakpoint directly to your #Python code using `pdb.set_trace()`

Here is an example:
Starting in #Python 3.7, you can use the `breakpoint` keyword instead of `pdb.set_trace()`

If you run this code in PyCharm, VS Code and other popular debuggers, the IDE's debugger will launch instead of `pdb` when it reaches the `breakpoint()`
You can read more about the `breakpoint` keyword in PEP 553

python.org/dev/peps/pep-0โ€ฆ
If you can't remember a `pdb` command, just type `help` to get a full listing
This series of tweets is based on my tutorial @mousevspython

blog.pythonlibrary.org/2020/07/07/pytโ€ฆ
The full code examples can also be found on my PyTips repo:

github.com/driscollis/pytโ€ฆ
Thanks for reading my thread on Python's debugger, `pdb`!

Follow me to learn more Python!

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

19 Nov
Today we are going to talk about the basics of unit testing in #Python

๐Ÿงต๐Ÿ๐Ÿ‘‡
The Python programming language includes built-in packages for testing:

๐Ÿ doctest
๐Ÿ unittest
I already did a thread on Python's `doctest`:

Read 13 tweets
17 Nov
One big gotcha when it comes to #Python decorators is that the decorator will covertly replace the function's name and docstring with its own (๐Ÿงต๐Ÿ๐Ÿ‘‡)

Check out the following example Image
When you run the decorated code in the previous tweet, you will see that the decorated function's name and docstring have disappeared! Image
You may be wondering, why do I care if a function's name and docstring are replaced?

The reason this is important is that your introspection won't work correctly anymore. If you run Python's `help()` built-in against your decorated function, it will give you the wrong info
Read 7 tweets
17 Nov
#Python has had the concept of context managers for a loooong time!

Let's talk about context managers again!

๐Ÿงต๐Ÿ๐Ÿ‘‡
The `with` statement, which is the normal way for working with context managers, was added back in Python 2.5!

Here is a pretty common example of using a context manager: Image
The beauty of a context manager is that they allow you to do some setup and teardown automatically.

The downside is that is abstracted away and can sometimes make the code less obvious when debugging
Read 8 tweets
15 Nov
Logging in #Python is a popular topic and it's a really useful one to understand.

Today we are going to learn more about that!

๐Ÿงต๐Ÿ๐Ÿ‘‡
Python includes the `logging` module as a part of its standard library.

You can get started using it quite easily:
Python's `logging` module supports multiple logging levels:

๐Ÿ notset
๐Ÿ debug
๐Ÿ info
๐Ÿ warning
๐Ÿ error
๐Ÿ critical
Read 17 tweets
12 Nov
One of the harder concepts to learn in #Python are decorators.

So let's take a few moments and learn about them!

๐Ÿงต๐Ÿ๐Ÿ‘‡
It's always good to start with a regular function. Why?

Because you create a decorator using functions!

This function, `doubler()` takes in a number and doubles it. That's it! Image
But wait! #Python functions are also objects. You can get their `__name__` and their docstring (`__doc__`), if they have one.

You can also get a listing of their other methods with `dir()`

What all this means is that you can pass a function to another function! Image
Read 11 tweets
8 Nov
Did you know you can watermark images with #Python?

Find out how using the #Pillow package in this mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
If you want to add text, such as a URL, to an image, you can do that pretty easily with #Python and #pillow

The following code snippet is only 21 lines!
Here are the before and after photos of the Yequina lighthouse showing how to add a watermark with #Python
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(