"Import guard" in julia

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

See Within a Julia script, can you tell whether the script has been imported or executed directly? - Stack Overflow

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.

Try something like if PROGRAM_FILE == @__FILE__ ?

1 Like

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

Running into problems with this as the PROGRAM_FILE is the relative path and the @_FILE__ is the absolute path. Running from command line using

julia --project=. bin/foo.jl

See Julia Python equivalent of __main__? - #2 by fredrikekre.

1 Like