"Import guard" in julia

question

#1

Hi all,

Apologies if I’m lacking google skills, but I’m unable to find a Julian equivalent of the following common python pattern:

if __name__ == "__main__":
    run_my_main_func()

This construct prevents code being executed when import-ing in python.

I’m developing a few bespoke scripts in julia, with nearly all functionality separated into bite-sized functions. For debugging purposed, I’d like to include("thescript.jl") and run functions by hand, but I have main(ARGS[1], ARGS[2])-style code in there for CLI usage.

Cheers,
Kevin


#2

See http://stackoverflow.com/questions/14462557/within-a-julia-script-can-you-tell-whether-the-script-has-been-imported-or-exec


#3

Thanks, and sorry for my inability to google!

EDIT:

It looks as though that is specific to the REPL. While this solves my current issue, like the original stack overflow poster, I’d like to know if there is a way to tell if a file has been include()-ed or imported.


#4

Try something like if PROGRAM_FILE == @__FILE__ ?


Export and run in console
#5

Between the two, that looks like it will work. Thanks both!