This is a Julia related question. But to give some context, I’m not the best programmer, so my python workflow is to write some half complete code in a function, then debug the code in pycharm and use the interactive debug console to help me figure out how to complete the function. For example
def cartesian_product():
a = ['1', '2', '3']
b = ['a', 'b', 'c', 'd']
# I want to compute the cartesian product of two lists but I don't quite know how
# it's done so I google a bit attach the debugger here and explore the various
# approaches eventually settling with the itertools.product() approach.
return list(itertools.product(a, b))
Now I was wondering if there is a similar approach possible in Julia using visual studio code?
When I create my semi complete julia function
function cartesian_product()
a = ['1', '2', '3']
b = ['a', 'b', 'c', 'd']
# attach debugger here and try figure out the rest using the vs code interactive
# debugger
end
The problem with this approach is that I can’t create new variables in the debug console. Possibly because julia is a compiled language?
For example if I set a breakpoint at the b
statement, the variable a
has loaded in memory but b
has not. So in the debug console I define b
just like in the python debug console. But now when I try to referrence b
I get an UndefVarError: b not define
So my question is if this type of workflow is not possible (that is figure things out in the debug console), what are the alternatives?
I’ve tried these approaches:
- Write code in
.jl
files. Then run them in the REPL – but this kind of get’s messy when you have lot’s of custom modules and functions (setup code) that run before the point of the code you are interested in. - Had a quick look at
revise
but I don’t think it quite does what I want.
Do I just have to suck it up and adopt a different approach to programming?
2
Answers
If you want to experiment or code something up quickly in an interactive environment, you may want to use the Julia REPL as your debug console, especially if you have time to learn some of its more advanced features.
But if your code already spans several modules, it should already have its own
.jl
file, if not its own package. If the code you’re actively working on depends on those modules and functions, there’s no getting around that, but you can supress output to make it less messy.You could try
using Debugger
than you can use@enter
macro:With this tool you can interact with running code and in particular manipulate the values of variables (to go to julia mode press apostrophe “` and ENTER). See this sample session:
You can see that the
c
variable was mutated.The full set of options is described at https://github.com/JuliaDebug/Debugger.jl.
Another good route is to use Visual Studio Code and breakpoints as described in https://www.julia-vscode.org/docs/stable/userguide/debugging/
In IDE debugger you can change variable values in the runtime – just double click the variable when being stopped on a breakpoint: