Julia Python equivalent of __main__?

In Julia, what is the python equivalent of if name == “main” ?
For example:

def main():
    pass

def print_tre_volte(parola):
    print(parola)
    print(parola)
    print(parola)

if __name__ == "__main__":
    main()

It the script is run directly, it will does main(), otherwise no.

In Julia?

1 Like

See https://docs.julialang.org/en/v1/manual/faq/#How-do-I-check-if-the-current-file-is-being-run-as-the-main-script?-1. Something like the following should work:

function main()
   # do stuff
end

if abspath(PROGRAM_FILE) == @__FILE__
    main()
end
17 Likes

It works, thanks!

I know (emh, from yesterday… ) that in julia there is not the script-mentality as in python, but, you know, for a beginner, this is very simple to use …

1 Like

This solution does not work when using vscode. When I print abspath(PROGRAM_FILE) and @__FILE__ I get

/Users/user/.vscode/extensions/julialang.language-julia-1.6.17/scripts/terminalserver/terminalserver.jl

/Users/user/pathtofile/main.jl

Is there a way for this to work in vscode?

This solution was meant to run the script only if it was called directly and do nothing otherwise. VSCode does not call the script directly, so it does nothing, as it should. Why do you need the if? Can’t you just:

function main()
   # do stuff
end

main()