11

What can I do within a file "example.jl" to exit/return from a call to include() in the command line

julia> include("example.jl")

without existing julia itself. quit() will just terminate julia itself.

Edit: For me this would be useful while interactively developing code, for example to include a test file and return from the execution to the julia prompt when a certain condition is met or do only compile the tests I am currently working on without reorganizing the code to much.

6
  • 1
    Can you explain in detail what the actual problem your trying to solve is? We can probably provide better answers if you do, since it doesn't sound like you're using include as intended. Commented Feb 5, 2016 at 14:24
  • 3
    You could raise an exception with error or throw. Commented Feb 5, 2016 at 15:30
  • I originally had exception as part of my answer but I removed it. I think that is (probably) a butchering of the try..catch control path ... perhaps, with more detail, that might be a more reasonable solution however. Commented Feb 5, 2016 at 16:01
  • 1
    Raising an exception is a good idea because it breaks out to the right level. It would be nice to do the same without abusing the exception control path, but that is working for me. Commented Feb 5, 2016 at 16:34
  • I used error("stopping") successfully to get out of the execution of the script without exiting the julia environment as @VincentZoonekynd, recommends. Commented Aug 10, 2017 at 16:14

4 Answers 4

3

I'm not quite sure what you're looking to do, but it sounds like you might be better off writing your code as a function, and use a return to exit. You could even call the function in the include.

Sign up to request clarification or add additional context in comments.

2 Comments

If the function was called from another function, it will return control to the function further up, without actually proceeding to stop executing statements causing a genuine error possibly .
The question is about running .jl files from the REPL, with breaks from arbitrary points.
3

Kristoffer will not love it, but

stop(text="Stop.") = throw(StopException(text))

struct StopException{T}
    S::T
end

function Base.showerror(io::IO, ex::StopException, bt; backtrace=true)
    Base.with_output_color(get(io, :color, false) ? :green : :nothing, io) do io
        showerror(io, ex.S)
    end
end

will give a nice, less alarming message than just throwing an error.

julia> stop("Stopped. Reason: Converged.")
ERROR: "Stopped. Reason: Converged."

Source: https://discourse.julialang.org/t/a-julia-equivalent-to-rs-stop/36568/12

2 Comments

This is actually the best I have found so far. I have been using error or throw, but the stack trace is really jarring.
This actually still provides a stack trace, though just one line: ERROR: LoadError: "Stop." in expression starting at /path/to/file/julia-script.jl:51. Still, pretty good thanks!
1

You have a latent need for a debugging workflow in Julia. If you use Revise.jl and Rebugger.jl you can do exactly what you are asking for.

You can put in a breakpoint and step into code that is in an included file.

If you include a file from the julia prompt that you want tracked by Revise.jl, you need to use includet(.

The keyboard shortcuts in Rebugger let you iterate and inspect variables and modify code and rerun it from within an included file with real values.

Revise lets you reload functions and modules without needing to restart a julia session to pick up the changes.

https://timholy.github.io/Rebugger.jl/stable/

https://timholy.github.io/Revise.jl/stable/

The combination is very powerful and is described deeply by Tim Holy.

https://www.youtube.com/watch?v=SU0SmQnnGys

https://youtu.be/KuM0AGaN09s?t=515

Note that there are some limitations with Revise, such as it doesn't reset global variables, so if you are using some global count or something, it won't reset it for the next run through or when you go back into it. Also it isn't great with runtests.jl and the Test package. So as you develop with Revise, when you are done, you move it into your runtests.jl.

Also the Juno IDE (Atom + uber-juno package) has good support for code inspection and running line by line and the debugging has gotten some good support lately. I've used Rebugger from the julia prompt more than from the Juno IDE.

Hope that helps.

1 Comment

Thanks for sharing, this is good. However this is a very heavy weight solution to something meant to be very casual in the questioners use-case. And mine, that's why I like @mschauer own answer.
0

@DanielArndt is right.

It's just create a dummy function in your include file and put all the code inside (except other functions and variable declaration part that will be place before). So you can use return where you wish. The variables that only are used in the local context can stay inside dummy function. Then it's just call the new function in the end.

Suppose that the previous code is:

function func1(...)
....
end

function func2(...)
....
end

var1 = valor1
var2 = valor2
localVar = valor3  
1st code part
# I want exit here! 
2nd code part 

Your code will look like this:

var1 = valor1
var2 = valor2

function func1(...)
....
end

function func2(...)
....
end


function dummy()    
  localVar = valor3  
  1st code part
  return  # it's the last running line! 
  2nd code part 
end 

dummy()

Other possibility is placing the top variables inside a function with a global prefix.

function dummy()    
  global var1 = valor1
  global var2 = valor2
  ...
end 

That global variables can be used inside auxiliary function (static scope) and outside in the REPL

Another variant only declares the variables and its posterior use is free

function dummy()    
  global var1, var2
  ... 
end 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.