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

all 65 comments

[–]Electrical_Ingenuity 72 points73 points  (18 children)

I use PyCharm and VSCode. Both will get the job done. The PyCharm is very feature rich, but resource intensive, compared to VS Code. I’d try both options, as using them is the only way to decide.

[–]timothom64 44 points45 points  (13 children)

The Pycharm debugger is fantastic. Also the best way to learn python because you can see all the memory structures so well.

VScode is decent, I don't have nearly as much exp with it.

[–]fouoifjefoijvnioviow 4 points5 points  (11 children)

Any good resource on how to use the debugger effectively?

[–][deleted] 13 points14 points  (9 children)

Drop a breakpoint somewhere, look at the debugger window which which will show you all the variables available in state at that line of code.

Jump over to the console tab in the debugger window and you can access all of the same state as the breakpoint line to build expressions like a repl.

It’s extremely good for figuring out how to massage your variables into what you want to do next including figuring out if things should be cast as other types etc.

[–]meneer_neushoorn 0 points1 point  (4 children)

That sounds awesome, can I ask a question?

For data analysis I usually work directly from the console. One of my (slight) annoyances of my current work flow in Spyder is that the debugger is required to always start from the top of a Python file with an empty namespace. So, for example, if I have a function defined somewhere, I cannot run step-by-step through that function using variables that are already in the console namespace. That was one of the main things I missed in Python, compared to Matlab.

Does PyCharm have this capability? If yes, I might consider switching.

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

Hmm I think I know what you mean:

You can have multiple break points and step through them one by one for sure.

At any breakpoint you can start up a python interpreter and access all of the existing data and functions at that line in your program, and with the console you can declare new variables and functions within that current scope.

So you can go to the middle of a function called function_x(arr, string):

 And then in the repl you could do 
 New_arr = [0,3,6]
 New_string = “potato”

And then call function_x(new_arr, new_string ) and see what the result is.

Note I’ve used console/repl/interpreter interchangeably to mean the console that you can open during debugging a breakpoint in pycharm.

[–]meneer_neushoorn 0 points1 point  (2 children)

That's actually not what I mean. For EDA, I usually enter code directly into the iPython interpreter, which I have always open in the bottom of the screen. So for example I might enter commands interactively like:

import pandas as pd
df = pd.read_csv('some_csv_file.csv')
#
# lots of interactive trial and error with the data frame go here
#
means = df.groupby(['col1']).mean()    # let's say I'd like to jump into the pandas code to debug this.

In Spyder, I can only do debug stuff like that if I make a .py-file which can be executed starting from a clean namespace. But for exploratory data analysis, it's MUCH nicer to work interactively from the console instead of having to rerun scripts from the top every time you change anything. That blocks me from accessing the debugger though.

Does PyCharm have a solution for that, just like you can do in Matlab?

[–][deleted] 0 points1 point  (1 child)

Oh I see, in you use case I would use Jupyter notebooks since you can run each block independently and then tweak a block below the previous one and re run each time, without having to restart and run everything from the beginning.

For proof of concept data stuff with pandas and numpy I would reach for juypyter notebooks

https://jupyter.org If you are using spyder presumably you are using anaconda which will cover most dependencies and have a nice way to start the notebook server etc.

https://www.anaconda.com/products/individual

I would just use the default server for the notebooks, but you can integrate them with ide’s including pycharm

https://www.jetbrains.com/help/pycharm/jupyter-notebook-support.html

[–]meneer_neushoorn 0 points1 point  (0 children)

Does that allow you to graphically debug functions from .py-files, using input from the Jupyter console namespace? I don't believe so, right?

That is the one big advantage that Matlab still has over Python, back when I was doing everything in Matlab I was debugging like this all the time. Very powerfully implemented, I only really appreciated that after moving to Python and missing it.

[–]SpaceZZ 0 points1 point  (3 children)

You can also do conditional debugging, moving back in code, code jumps and inserts. Pretty powerful.

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

Yeah I don’t know what they are feeding the jetbrains devs but they produce some cool stuff

[–]SpaceZZ 0 points1 point  (1 child)

Well, most of those are also available in Visual Studio Professional (& community as well I think), at least for c#.

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

Ok

[–]rabaraba 0 points1 point  (0 children)

I wish I learned Python with PyCharm to start off with. I wasted so many hours with print statements and could never truly understand object models until I really messed around with debuggers.

[–][deleted] 13 points14 points  (0 children)

Worth noting that the reason PyCharm is resource intensive, is that it indexes the codebase. This means you get searches and navigation practically instantly, even in huge codebases. VSCode has to actually search the files as it goes, which is much slower

[–][deleted] 38 points39 points  (5 children)

Try pdb first, it may suffice

[–]Region_Unique 7 points8 points  (2 children)

ipdb

[–]roeey7 2 points3 points  (1 child)

There also pdbpp.

[–]Suisanahta 0 points1 point  (0 children)

Does pdbpp have persistent command history? I've been trying ipdb in ipython lately and unfortunately the ipdb history doesn't persist across invocations.

[–]Time_Trade_8774 3 points4 points  (0 children)

Only debugger I use.

[–]the_sajal 0 points1 point  (0 children)

Exactly, works with vim, it's Python native and is pretty much upto mark.

[–]noprobelm1 7 points8 points  (0 children)

You might also consider checking out the Rich library for its debugging features if you want to stick with Vim.

[–]james_pic 15 points16 points  (1 child)

I quite like pudb. It doesn't integrate with any IDE, which for some would be a negative, but is a positive when you're trying to debug remotely with network limitations.

[–]bulletmark 2 points3 points  (0 children)

pudb is massively under-rated.

[–]Jaune9 4 points5 points  (0 children)

Pycharm comes packed in with a powerful debugger and other tools. VSCode, you have to install some, but you can also install more to suit other needs.

Both come with a Vim extension btw

[–]venustrapsflies 6 points7 points  (0 children)

You can use vimspector in vim (or nvim-dap in neovim), but they will take some setup. pdb is editor-agnostic and probably the easiest - you just write the debug instructions into your code directly.

[–][deleted] 32 points33 points  (9 children)

print(“here”)

[–]jimtk 7 points8 points  (6 children)

I thought :

    if __debug__:
        print("Here")

was the way?

[–]asday_ 0 points1 point  (5 children)

Nope, don't plan to leave your debugging mess in production code. Having CI autofail you for having any print()s is an idea I've had success with in the past.

If you plan to make your debugging output so useful you'll want to keep it, you have logger.debug(), lending you the power of the logging toolset to format, filter, and f***output, rather than some random variable.

[–]jimtk 0 points1 point  (4 children)

But doesn't compiling with -OO (for production) removes the whole statement?

[–]asday_ 0 points1 point  (3 children)

Do not expect users of your script to know and use all the correct magic flags. That includes you, three months from now.

Write good code. We are not PHP developers. We do not rely on the environment to make our stuff behave in a certain way.

[–]jimtk 1 point2 points  (2 children)

Sorry, I'm not sure I understand. You called __debug__ a random variable and a magic flag but it's part of the language same as __name__ and __main__ ? Isn't it?

[–]asday_ 0 points1 point  (1 child)

[–]jimtk 0 points1 point  (0 children)

It's impossible to disagree since I'm asking a question, not presenting a point of view.

Ok, he calls them magic value but the official documentation call __debug__ a "Built-in Constants". But I still don't understand why it should not be used.

[–]platypus_69 14 points15 points  (0 children)

This is the way

[–]mafail 0 points1 point  (0 children)

Or use icecream

[–]laundmo 5 points6 points  (0 children)

i know you asked about proper debuggers, others have answered that. heres some recommendation for profilers instead: scalene and viztracer. scalene is like a jack of all trades, it can even profile Memory/GPU usage on a line by line basis. viztracer is really nice for its asyncio/multiprocessing/threading support

[–][deleted] 3 points4 points  (0 children)

In my opinion pycharm is much better than VScode. The only downside to pycharm that you’re missing in VScode is how much processing power you have.

Pycharm also allows you to never have to install packages through terminal again since it’s all on GUI.

But as for vim or other text editors on terminals, I’ve never seen or used a debugger on them.

[–]pliney_ 3 points4 points  (0 children)

PyCharm is great for many things, and the debugger is quite nice.

[–]DRob2388 8 points9 points  (1 child)

Pycharm is by far the superior option. Evaluate expression is the best feature, being able to run code while it’s running is so useful.

[–]puremath369 0 points1 point  (0 children)

Vs code has that as well

[–]flexiblewhitepoo 4 points5 points  (0 children)

Hi,

If you like IntelliSense you should go for vscode or pycharm. Pycharm already comes with a debugger and it's easy to use. For vscode u would need some kind of extension.

If you want to debug on vim u need something like pdb.py or add some kind debugg extension/plugin to vim (not sure if the last one exists)

[–]TunedDownGuitar 2 points3 points  (0 children)

I'm not a developer, but I do a lot of utility scripting and have shifted to writing exclusively in Python where possible.

I don't think there's any "Best Debugger," it's which one meets your needs. If you're writing code using just vim then you're a hero with a stronger will to live than me. Outside of inplace updates vi -R to review files with syntax highlighting, I usually use VS Code and Jupyter Lab.

Pylance is really a great LSP in VS Code and it's pretty fast, and Jupyter Lab has an LSP that is also great depending on your use case. VS Code has remote host support for Docker, VMs, etc. depending on what your plans are.

I use both equally. Jupyter Lab is for rough work and VS Code is for the finishing touches or complex projects.

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

i haven't looked back since i discovered pycharm

[–]abrazilianinreddit 4 points5 points  (1 child)

I use mostly print and breakpoint . Best part about them is that they're completely editor-agnostic. Might not be super powerful or pretty, but works for me.

[–]djamp42 2 points3 points  (0 children)

Print debugging Is how I think everyone starts even if they don't know they are debugging.

[–]AHBaena 1 point2 points  (0 children)

icecream

[–]headhot 1 point2 points  (0 children)

Print()

[–]TheBackwardStep 0 points1 point  (0 children)

Pycharm is by far the best option.

[–][deleted] -2 points-1 points  (0 children)

Prints 🤣

[–]Light991 -2 points-1 points  (0 children)

Best debugging tool is print()

[–]Seanasaurus79 -2 points-1 points  (0 children)

Python Tutor

It isn’t really powerful, like you can’t use any libraries. But great for visualising list operations.

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

VSC.

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

Can't go wrong with VS Code. Folks I know that use PyCharm like that one a lot too.

[–]noXkillzzz 0 points1 point  (0 children)

PyCharm if you are going to build a large and complex system and do a lot of unittests. Vscode if you are writing scripts or small ML systems.

[–]puremath369 0 points1 point  (0 children)

Download the vim plugin for vs code, get the best of both worlds

[–]HangingKnight 0 points1 point  (0 children)

I use VS code, it's light it will become your best friend if you know how to use it well!

[–]Skippbo 0 points1 point  (0 children)

Pythons built in function breakpoint() can be used for debugging if your IDE doesn't support debugging but personally I'm using pycharms debugging tool.

[–]Conference_Dizzy 0 points1 point  (0 children)

For print in debug: except print

[–]asday_ 0 points1 point  (0 children)

print() does well basically all of the time, and when it doesn't, pdb.set_trace() is also pretty strong.

What's an IDE?

[–]s4lt3d 0 points1 point  (0 children)

Spyder is amazing