Detect if running as script


#1

Does Julia have an equivalent to Python’s

if __name__ == "__main__":
    ...

Specifically, my problem is the following. I’m writing a standalone Julia script run_simulation.jl that does some computation and will ultimately run on a cluster. All the code in the script is in functions, including a function main(args), and the only line in the script that actually runs code is the last line

main(ARGS)

Now, during testing on small datasets, I’m getting a bit annoyed at the JIT-overhead. So, I’d rather run an interactive julia session, and do e.g.

julia> include("run_simulation.jl")
julia> main(["./runfolder"])

Then I can repeatedly run main without Julia recompiling.

This works if I comment out the last line of the script, but out of curiosity: can Julia detect whether it’s running as a script and should execute the main function, or whether it is included in the REPL (or another file)?


#2

Would isinteractive do what you need?

https://docs.julialang.org/en/stable/stdlib/base/#Base.isinteractive


#3

Yes it would! Thanks!


#4

Old x-ref: https://groups.google.com/d/msg/julia-users/ufpi8tV7sk8/-Uv0rtAWTWsJ


#5

Just be careful that it does not at all do the same thing as __name__ in python.