skip to Main Content

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

enter image description here

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:

  1. 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.
  2. 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


  1. 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.

    Login or Signup to reply.
  2. You could try using Debugger than you can use @enter macro:

    julia> @enter cartesian_product()
    In cartesian_product() at REPL[2]:1
     1  function cartesian_product()
    >2      a = ['1', '2', '3']
     3          b = ['a', 'b', 'c', 'd']
     4
     5      # attach debugger here and try figure out the rest using the vs code interactive
     6      # debugger
     7  end
    
    About to run: (Base.vect)('1', '2', '3')
    1|debug>
    

    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:

    
    julia> function printc()
           c=5
           println(c)
           end
    printc (generic function with 1 method)
    
    julia> @enter printc()
    In printc() at REPL[5]:1
     1  function printc()
     2  c=5
    >3  println(c)
     4  end
    
    About to run: (println)(5)
    1|julia> c=17
    17
    
    1|debug> n
    17
    In printc() at REPL[5]:1
     1  function printc()
     2  c=5
    >3  println(c)
     4  end
    
    About to run: return
    1|debug>
    

    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:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search