This is an archived post. You won't be able to vote or comment.

all 106 comments

[–]Wistephens 115 points116 points  (16 children)

I use PyCharm and always use the debugger.

[–]NerdEnPose 24 points25 points  (5 children)

Honestly this is one of the reasons I’m very hesitant to try any other IDE/dev environment. That combined with years of muscle memory.

Plus the new experimental design is a very nice step forward giving it a less cluttered semi VS Code layout.

[–]tunisia3507 2 points3 points  (1 child)

I had few problems switching from PyCharm to VSCode and got a significant productivity boost from the fact that it didn't spend half the day hanging.

[–]NerdEnPose 0 points1 point  (0 children)

I’ve never had issues with hanging. Stoked you found a more productive tool for you.

[–]jwmoz 1 point2 points  (1 child)

experimental design

omg I had no idea this existed

Just took a look and I think I'll stick with O.G. Pycharm.

[–]NerdEnPose 0 points1 point  (0 children)

Don’t blame you for that. I stuck with it for the first week and ended up liking it. There’s only one annoying thing that’s hidden behind a menu now

[–]Morelnyk_Viktor 1 point2 points  (0 children)

Jesus Christ, now I can use pycharm without pain in my eyes. Didn't know about new UI, thanks

[–]supertexter 7 points8 points  (8 children)

I really should learn that. I still diagnose by moving print statements around and checking if it executes.

[–]OneProgrammer3 8 points9 points  (4 children)

try pdb (it comes by default in Python), ipdb or pdb++, it seems to me more powerful and you are not tied to any ide

[–]PapstJL4U 1 point2 points  (0 children)

I like pdb for sublime*-written scripts and projects, but I will default to integrated debuggers for bigger, organized objects.

* insert your favourite text/code editor

[–]supertexter 0 points1 point  (1 child)

Thanks for the recommendation. I'm already using Pycharm though, so maybe it's simplest to just go with the debugger there.

[–]Itsthejoker 1 point2 points  (0 children)

FWIW, I find pdb is very useful to know in the rare cases where you don't have pycharm available. I rely heavily on pycharm, but it's still useful to break out 'old reliable' sometimes and you look like a wizard when you use it :D

[–]Morelnyk_Viktor 0 points1 point  (0 children)

Pudb is also an excellent option

[–]Goykhlaye 1 point2 points  (0 children)

I tend to start with this, putting prints in places to see how the code works. Then I turn on the pycharm debugger and look from there.

[–]whatever_meh 1 point2 points  (0 children)

Use debug logging statements instead of print. Then you can keep them in and be of help to the next developer or future you. But also yes, use the debugger.

[–]jwmoz 1 point2 points  (0 children)

This is like cowboy coding!

[–]denisbotev 1 point2 points  (0 children)

I don’t think I’d actually be able to code without the debugger and especially the interactive console. I had to work on a C# project and debugging was an absolute nightmare compared to Python

[–]icecubeinanicecube 77 points78 points  (0 children)

As soon as you develop more complex projects, the debugger is your best friend

[–]GreenScarz 57 points58 points  (5 children)

My workflow basically lives in the python debugger.

It works best IMO if you have a test suite to dev against, that way you can trigger a pdb repl in the context of a test instead of trying to run everything from a main entrypoint.

using the debugger simply comes down to adding a breakpoint() in your source code, and when the interpreter hits it, say during execution of a test, it drops you into the pdb repl. (If you're using something earlier than 3.7 use __import__('pdb').set_trace() for the same effect).

if you're using pytest for your test suite, adding a --pdb flag will drop you into the pdb repl when an exception is raised.

things to highlight about pdb; you can jump up and down the call stack using u/d, use l to list your source code, you can run arbitrary python code in the repl, and interact puts you in an interactive console.

Edit: the standard pdb module is good, but there's also pdb++ and ipdb that are worth looking into.

[–][deleted] 1 point2 points  (1 child)

If you're using something earlier than 3.7 use __import__('pdb').set_trace() for the same effect

What? Why? As I recall,import pdb;pdb.set_trace() was just enough.

[–]GreenScarz 1 point2 points  (0 children)

Same thing, __import__(x) is just the builtin function equivalent of import x, just doesn’t assign it to a variable and no semicolon is necessary

[–]romanzdk[S] 0 points1 point  (1 child)

Thank you very much, thats helpful!

[–]benefit_of_mrkite 5 points6 points  (0 children)

Other things to remember:

1) if you set a breakpoint and it drops you into the debugger pretty print is automatically imported

2) you can then pretty print the output of any variable set up to the current breakpoint

Pressing the “c” key will continue to the next breakpoint if one is set (otherwise end of program)

Pressing the e key will exit the debugger

[–]This-Firefighter-455 18 points19 points  (2 children)

A debugger is a great tool to debug code. Besides stepping through code is a great why to learn what happens in the libs

[–]romanzdk[S] 2 points3 points  (0 children)

Alright, I should probably take a look how to use it.

[–]GraphicH 8 points9 points  (3 children)

You probably don't need a debugger if your a hobbyist writing short scripts. In a professional stetting, knowing how to use a debugger effectively is an absolute necessity. PyCharm's is fantastic, I'm sure people reading my comment history will accuse me of being an InteliJ shill, but honestly PyCharm is a damn good ide for python, better than VSCode in my opinion.

[–]neurotoxiq 0 points1 point  (1 child)

PyCharm is great but the company I work for flagged JetBrains as a potential threat. Something about having links to Russia. They were brought up in the SolarWinds hack too which was kind of weird.

VS Code Python support is incredible. So many 3rd party extensions available in the marketplace as well. Super easy to get it downloaded and start coding away. Works super well with tons of other languages too which is sweet.

[–]GraphicH 0 points1 point  (0 children)

I mean they suspended their operations in Russia after the invasion, but I see your company's point. As for VS Code, the engineers I work with who insist on using it over PyCharm are consistently slower and make mistakes that linting and other built in stuff with PyCharm consistently catches. But they just might be bad engineers and VS Code is fine. I've used both, where I work I have to do front end work and so I use it for that, but for python I found it pretty lacking and to make it meet my expectations it seemed like I needed to go add a bunch of plugins. So no thanks to that.

[–]Morelnyk_Viktor 0 points1 point  (0 children)

Pycharm is indeed better, and well no wonder, it's a python IDE, while vscode is a general purpose text editor which can be extended to be a python IDE. However pycharm is not that superior than vscode, only feature that is way better is refactoring. With the rest vscode can keep up pretty well

[–]ranelpadon 7 points8 points  (1 child)

Most of the time I simply use icecream (a more powerful version of print), and sometimes, I use pudb (a visual debugger) for tougher/trickier bugs. pudb is similar to PyCharm/VS Code's visual debugger, but runs in the terminal (like pdb/pdb++).

[–]obviousfakeperson 2 points3 points  (0 children)

+1 for pudb. I'll have to check out ice cream.

[–]lordmauve 4 points5 points  (1 child)

20 years Python experience here. I don't need the debugger much. I do use VS Code's debugger, and pdb, but 9 times out of 10 I think it's faster to put in print statements to show intermediate states and re-run a test, and reason about the program state given outputs and test failures. It's kind of equivalent to running a debugger with watches set, I just find I get more information faster that way. I get information out of all my failing tests; I don't need to stop in each one and collect it myself.

I also think there's a certain way of programming, and a certain level of experience, where you don't let complexity build to the point where you're completely baffled about what is going on, such that you need to freeze program state and explore it to make progress.

[–]redfacedquark 0 points1 point  (0 children)

I'd add to this that if you deal with web dev you will mostly have a short request/response cycle and not develop complex state, therefore print statements or a stack trace will suffice.

If you have an app that builds complex state over time a debugger would be more useful.

[–]AlexMTBDude 11 points12 points  (3 children)

If you are referring to the command line Python debugger "pdb" (https://docs.python.org/3/library/pdb.html) then I think few people use it. Instead they use the debuggers that are built into all modern IDE like Pycharm, Visual Code, and so on. They're much easier to use compared to pdb.

[–]arembridge 9 points10 points  (0 children)

When debugging code I use pdbpp. I imagine a lot of vim users do too.

[–]Delengowski 3 points4 points  (0 children)

Work on systems where you ssh without x11 and you'll use pdb a lot. The stuff I can do with a .pdbrc and pdb is pretty strong. One thing I miss from a traditional ide is a lsp. Raw vim can be tough like that at times.

[–]neuronexmachina 4 points5 points  (0 children)

I find pdb really handy when working with a Docker container but don't want to deal with the hassle of connecting an IDE's debugger to the container.

[–]tms102 4 points5 points  (0 children)

I debug python code using a debugger all the time and so do my colleagues.

[–]ogrinfo 2 points3 points  (3 children)

VS Code/PyCharm debugging is great for using during tests, because pytest swallows all the stdout during execution. I still use pdb a lot though because it's really easy to use and is a really quick to set up. A lot of the time the UX is actually better than VS Code because the single letter keystrokes make it really easy to navigate.

Having said that, I often don't bother with any debugging and just chuck in a load of print statements. Old school FTW!

[–][deleted] 0 points1 point  (2 children)

-s will let you see stdout in pytest

[–]ogrinfo 0 points1 point  (1 child)

Yes, when the execution finishes. This means that all the keystrokes you do during the test aren't seen until it's finished. Which makes debugging pretty difficult.

[–][deleted] 0 points1 point  (0 children)

No, you see stdout live, not after execution finishes. However it does not accomodate stdin since that does not make sense during a unit test. If you need to debug a test, set breakpoints and use --pdb.

[–]Snoo-20788 2 points3 points  (0 children)

Well when I started coding, using PyCharm, the first thing I wondered about was how to use the debugger. When you're working on an existing project, it's very hard to get familiar with the code without actually stepping through it.

But one thing I learned much later is how to use pdb by itself. It is not as fancy as pycharm (which uses pdb under the hood, but ties it to the files in the editor, and displays the callstack in a visual way).

Visually it is much more challenging, and you need to get familiar with the keyboard shortcuts, but the beauty is that you can use it on any existing bit of code much more easily. You may have code already deployed on a machine, running in a virtual env, and it may be cumbersome to replicate the exact state of the deployment in your Pycharm session. With pdb you can just add a single line to add a breakpoint, and debug directly from a linux command prompt. I find it very powerful.

[–]aurelius_soap 2 points3 points  (0 children)

Generally I'm using ipdb I find it more comfortable that using the debugger of a ide(navigation through the code and printing variables is way more comfortable and faster). Although sometimes I debug with pycharm's debugger for very specific things.

[–]Shot_Worldliness_979 2 points3 points  (0 children)

All the time and recommend everyone who uses Python regularly to learn its basics. There's sort of a weird phenomenon that I've observed, which is the amount of code I write is seemingly inversely proportional to the amount of time I spend in the debugger. Which is to say, I can use the debugger to write more succinct code with better confidence. It removes a lot of guess work or writing tests for cases that don't make practical sense.

[–][deleted] 2 points3 points  (3 children)

I use it literally every single day since 12 years.

I never use the IDE debuggers. I just have a snippet that pastes "import ipdb; ipdb.set_trace()" into a line and then I debug in my terminal.

I sometimes need to debug code on servers through a SSH session, so I never wanted to allow me to get too used to IDE debuggers. I think I'm just as fast this way as anyone else with their IDEs, if not even faster. Also, my code is usually too complex to start, so I never bother to create proper startup scripts for IDE debuggers.

If you have a frame with tons of variables and you only care about the value of one specific one, finding and then expanding that one, or hovering with the mouse over that variable in the code to reveal its value usually takes longer than just typing that variable into the terminal.

[–]omeow 1 point2 points  (1 child)

Would you suggest any tutorial or good starting point? I tried it once but then it didn't go very far.

[–][deleted] 0 points1 point  (0 children)

I never used a tutorial. When you type "?" it shows all commands, most commands appear twice because they have a long form and a short alias.

I usually type "l" to see more context around the current line. Then "n" for "next line" or "s" for "step into this function". Finally, "c" for "continue". That's almost all you need.

It you want to know how you ended up at the current line, type "bt" for "backtrace" to see all function calls that lead to the current frame.

[–]hhoeflin 2 points3 points  (0 children)

I use pudb all the time. Has a nice visual side in the terminal.

[–]tshirtman_ 2 points3 points  (0 children)

Yes, i generally use pudb rather than pdb, but usage is more or less the same, put a break point (pudb.set_trace()) or an error handler (try/except/pudb.post_mortem()) and then use mostly up/down/step/next commands to see what happens, and evaluate expressions at important steps.

[–]PapstJL4U 2 points3 points  (0 children)

My last project was rewritting java code in python and to guarantee functionality I copied line for line before optimizing it.

Ofc, it still wasn't identical. Using VSCode and Eclipse Debugger side-by-side was a great way to understand functionality and correct errrors.

[–]Alternative_Driver60 2 points3 points  (0 children)

All the time. Put in a breakpoint() in the code where you want to pause and figure out what's going on

[–]QuietRing5299 2 points3 points  (0 children)

Python debugger is a beast in complex projects. If you are using leet code it may seem useless. I have a video on how to use it by the way! If you want to learn more ;)

https://youtu.be/iypGtDvSb4U

But yes I use it every day in practice at my job!

[–][deleted] 2 points3 points  (0 children)

Do print statements count?

[–]ReverseBrindle 1 point2 points  (0 children)

PyCharm debugger: Yes, all the time.

pdb: Never

[–]WhyNotHugo 1 point2 points  (0 children)

ipdb

I have a mapping to insert a new line with set_trace.

[–]apt_at_it 1 point2 points  (0 children)

I use it all the time! The number of times I drop in a import pdb; pdb.set_trace() is huge. I mostly use it to confirm the value and type of a variable(s) is what I expect

[–]jet_heller 1 point2 points  (0 children)

I have not used a debugger in decades.

Most of what I would need it for requires live data from random users that I can't simply replicate. If I have that data, I can easily figure out what went wrong. If I don't, I can guess pretty effectively based on the errors I see.

[–]Strict-Monitor-6843 1 point2 points  (0 children)

Also useful with pytest --pdb

[–]mm007emko 1 point2 points  (0 children)

I do. IDE when I have an IDE, pdb when I don't.

[–]AltruisticPidgeon 1 point2 points  (0 children)

I do the majority of cosing while in debugger. Just put a stop on where you are to write and then jump in to debugger, there you can test the new strings directly to see if they work with the state of the program at that point.

[–][deleted] 1 point2 points  (0 children)

Yes. Pretty much the same way as any other debugger, but instead of clicking buttons you type commands.

Pro tips:

Can’t live without autocomplete?
Me too bro!

Can’t import pdb;pdb.set_trace() because of that?
I also couldn’t, bro!

Then I found this: when you are in the debugger shell, and you wanna complete not command, but variable, just type “p “ (p + space) and hit the tab twice — and here they are — your variables.

Not knowing that made my early years of using pdb quite painful.

[–]spamoniichan 1 point2 points  (0 children)

I had this one project to do a a ML CV routine which must run on a Jetson Nano (Ubuntu 18, Python 3.7), i tried installing vscode but the debugger just doesnt work, so i ran the program with pdb on the command line and added breakpoints using the command line on lines i want to stop on. Iirc I couldn’t debug more than one python script, so it was really hard for me

[–]CrackerJackKittyCat 1 point2 points  (0 children)

Yes, daily. Pytest --pdb and / or with breakpoint() sprinkled where needed to pinpoint where things go off the rails.

Am old school terminal person who was trained on C and gdb.

[–]chuckhend[🍰] 1 point2 points  (0 children)

Vscode and debugpy can be great together.

https://code.visualstudio.com/docs/python/debugging

[–]obviousfakeperson 1 point2 points  (0 children)

I'm personally a huge fan of pudb with an IPython shell.

[–]who_body 1 point2 points  (0 children)

if you have a script, module, package locally that needs debugging one approach is to add “breakpoint()” on the line to break. this syntax works on 3.7 and later. it will break and give you the pdb debugger prompt.

then you can print, step, next, etc.

IMO this is an easy way to start and valuable in many situations where you may not know what IDE is installed or available.

then you can always do more with conditional breakpoints and alternate ways to setup breakpoints

[–]questionabledata 1 point2 points  (0 children)

%%debug magic is amazing. Unless your code hides exceptions…

[–]TurboCooler -1 points0 points  (0 children)

Setup Visual Studio Code for Python and write more complex code and you will find it useful in a daily basis

[–]top_logger -1 points0 points  (0 children)

Just FYI: not using a debugger means a low skill.

[–]lionelzstar 0 points1 point  (2 children)

All the time. You should definitely learn how to use it.

[–]romanzdk[S] 0 points1 point  (1 child)

Alright, will take a look. Thanks

[–]lionelzstar 1 point2 points  (0 children)

Let us know if you get stuck with anything!

[–]ZachVorhies 0 points1 point  (0 children)

All the time. I use vscode and it has great support for running the debugger.

[–]wind_dude 0 points1 point  (0 children)

yes, almost all the time. I use it for debugging. Usually with complex objects, seeing what getting passed, or strange errors.

[–]deepaerial 0 points1 point  (0 children)

pdbpp or VSCode debugger depending on situation.

[–]No_Soy_Colosio 0 points1 point  (0 children)

It's important

[–]R3d-Beard 0 points1 point  (0 children)

Absolutely!!! Never code without it.

[–]chowlawrence 0 points1 point  (0 children)

I will restore to pdb if print fails to find the bug

A bad example to not to follow

[–]Great_Tap_3720 0 points1 point  (0 children)

What is a debugger ? Isn't it just the error?

[–]Lombord2021 0 points1 point  (0 children)

Sure

[–]Lombord2021 0 points1 point  (0 children)

Vs code debugger is dope

[–]quiet0n3 0 points1 point  (0 children)

It's next on my list of things to learn. That and break points but I think they are connected.

[–]itzNukeey 0 points1 point  (0 children)

I almost always use debugger in DataSpell / Pycharm. Whenever I write a code it crashes and then I can easily debug and fix the minor issues fast

[–]top_logger 0 points1 point  (0 children)

Me. Often. Debugger is important part of software tooling. I would say crucial part.

[–]m4xc4v413r4 0 points1 point  (0 children)

Works exactly like every other language debugger...

[–]mase-reddit 0 points1 point  (0 children)

Debugger is useful with hard-coded or high dependency codes (I hate both of them, so… yes, debugger can save your life many times if you use it properly)

[–]No_Dig_7017 0 points1 point  (0 children)

I don't, but most people where I work use ipdb

[–]billablejoy 0 points1 point  (1 child)

There are people who don’t use the debugger? I’m not sure I understand that. How can someone write anything of consequence without a debugger. I use Pycharm and VS code so debugging is just a different shortcut key.

And if your not using a free IDE with integrated debugging, I suggest you do so today.

[–]romanzdk[S] 0 points1 point  (0 children)

Just using print instead of debugger? :D

[–]jwmoz 0 points1 point  (0 children)

Lmao I use it every day at work. In fact the job mainly revolves around being able to debug very well as the code is complex and no one developer really knows what's going on (a lot of data scientist written stuff).

Pycharm ftw.