# How to import functions defined in a Julia script file without running the script itslef

**URL:** <https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603>\
**Category:** General Usage\
**Created:** [October 21, 2017, 9:52pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603 "2017-10-21T21:52:54Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 21, 2017, 9:52pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/1 "2017-10-21T21:52:54Z")

</div>

I would like to reopen this topic already discussed years ago at the following links:

[https://stackoverflow.com/questions/14462557/within-a-julia-script-can-you-tell-whether-the-script-has-been-imported-or-exec](https://stackoverflow.com/questions/14462557/within-a-julia-script-can-you-tell-whether-the-script-has-been-imported-or-exec)

[https://groups.google.com/forum/#!topic/julia-users/ufpi8tV7sk8/discussion](https://groups.google.com/forum/#!topic/julia-users/ufpi8tV7sk8/discussion)

I am used to write Python code, and I find the `if __name__ == ' __main__'` functionality very convenient in the Python open source environment, because it allows to reuse functions written by others (who maybe did not think they would have been useful outside their project) without modifying their code.

I am now dealing with a concrete case, which I submit here for advice. I am contributing to the [static-julia](https://github.com/JuliaComputing/static-julia) project, whose main [juliac.jl](https://github.com/JuliaComputing/static-julia/blob/master/juliac.jl) script calls the [julia-config.jl](https://github.com/JuliaLang/julia/blob/master/contrib/julia-config.jl) script to define the proper `gcc` compilation flags. To do this, since [julia-config.jl](https://github.com/JuliaLang/julia/blob/master/contrib/julia-config.jl) is thought to be directly executed (see function `main()`) and not to be included by other scripts, [juliac.jl](https://github.com/JuliaComputing/static-julia/blob/master/juliac.jl) has to use the following awkward code to get the flags:

```julia
command = `$(Base.julia_cmd()) $(joinpath(dirname(JULIA_HOME), "share", "julia", "julia-config.jl"))`
cflags = Base.shell_split(readstring(`$command --cflags`))
ldflags = Base.shell_split(readstring(`$command --ldflags`))
ldlibs = Base.shell_split(readstring(`$command --ldlibs`))

```

This of course is very inefficient.

In my mind Python’s `if __name__ == ' __main__'` functionality would be handy in this and similar cases. I would like to ask your opinion and advice on this.

Thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2017, 7:07am UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/2 "2017-10-22T07:07:10Z")

</div>

Split the code into two files, `main.jl`:

```julia
include("reusable_code.jl")
do_stuff_based_on(ARGS)

```

and `reusable_code.jl`, which contains the part you want to reuse. Even better, you should wrap it in a module.

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 22, 2017, 1:53pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/3 "2017-10-22T13:53:23Z")

</div>

The issue is that I am not the owner of the `julia-config.jl` script. To do that I would need to ask the maintainer of the project whom `julia-config.jl` belongs to (in this case Julia itself) to change his code for me. Apart from this specific case (which I do not know what the authors of the Julia would think about it), in general the owner of the project could refuse to change his code for me, at least not in time for my needs, or the project could even be unmaintained.

I think a function similar to `include` but which only includes functions and similar definitions without actually executing the script would be very useful (it could be named for instalnce `includef`).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2017, 2:17pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/4 "2017-10-22T14:17:39Z")

</div>

> [@lucatrv](#):
>
> I think a function similar to include but which only includes functions and similar definitions without actually executing the script would be very useful (it could be named for instalnce includef).

A function like that is impossible. There’s no difference between function definitions and other code. We don’t have a way similar to python for doing this because files are not tied to a module and so executing a script is nothing more than just `include` it. You don’t have to split the file to make part of it reusable though, you can just have a check for a certain global variable. In either case (python included) the script must be written to support this.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2017, 2:33pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/5 "2017-10-22T14:33:13Z")

</div>

> [@lucatrv](#):
>
> To do that I would need to ask the maintainer of the project whom julia-config.jl belongs to (in this case Julia itself) to change his code for me.

Are we talking about [this file](https://github.com/JuliaLang/julia/blob/master/contrib/julia-config.jl)? If you find functionality there that is useful in general, you should submit a PR that factors it out.

> [@lucatrv](#):
>
> in general the owner of the project could refuse to change his code for me, at least not in time for my needs

I fail to understand the issue. If you have the source, you do what you like with it, as quickly as you prefer.

> [@lucatrv](#):
>
> I think a function similar to include but which only includes functions and similar definitions without actually executing the script would be very useful (it could be named for instalnce includef).

This is not necessarily well-defined in general. Consider

```julia
const HASH_CUSTOMTYPE = 0xfd53ac3dfed72cdf
struct CustomType{T}
    value::T 
end
Base.hash(A::CustomType, h::UInt) = hash(A.value, hash(HASH_CUSTOMTYPE, h))

```

Is the first line part of the “script” or the “functions”? It is not a function, but necessary for the function to work. You will find similar examples with `@eval` etc all over the Julia codebase.

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 22, 2017, 3:38pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/6 "2017-10-22T15:38:55Z")

</div>

> [@Tamas\_Papp](#):
>
> Are we talking about this file? If you find functionality there that is useful in general, you should submit a PR that factors it out.

Yes, but this would work only if the author of [julia-config.jl](https://github.com/JuliaLang/julia/blob/master/contrib/julia-config.jl) accepts my PR to make my code work, but maybe he would not like to make his code more complex just to make mine work or simply does not care (of course I am taking [julia-config.jl](https://github.com/JuliaLang/julia/blob/master/contrib/julia-config.jl) just as a specific example but for a more general talk).

> [@Tamas\_Papp](#):
>
> If you have the source, you do what you like with it, as quickly as you prefer.

The drawback of doing this is that I would duplicate existing code in my project (every time a new version of [julia-config.jl](https://github.com/JuliaLang/julia/blob/master/contrib/julia-config.jl) is released I would need to copy and paste the relative code into my project).

> [@Tamas\_Papp](#):
>
> Is the first line part of the “script” or the “functions”? It is not a function, but necessary for the function to work. You will find similar examples with @eval etc all over the Julia codebase.

I agree to some extent, that is why I wrote “functions and similar definitions” (thinking in general about structures, constants, etc). I opened this topic to brainstorm on this, but I am still not convinced that there cannot be better solutions.

For the sake of discussion, what about an `includeonly` function which allows to specify what exactly to include among available definitions?  
For example:

```julia
includeonly("script.jl", "struct CustomType")

```

or

```julia
includeonly("script.jl", "CustomType::struct")

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2017, 4:19pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/7 "2017-10-22T16:19:30Z")

</div>

> [@lucatrv](#):
>
> For the sake of discussion, what about an includeonly function which allows to specify what exactly to include among available definitions?

That’s even more impossible to do…

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 22, 2017, 5:18pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/8 "2017-10-22T17:18:31Z")

</div>

Impossible in the general case, yes. Things might be defined through macros, etc, in ways that are impossible to detect without actually running the code. But it should not be to hard to parse a file and search for `expression.head == :type && expression.args[2] == :CustomType`, or something like that.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2017, 5:38pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/9 "2017-10-22T17:38:10Z")

</div>

If you really want that, you might as well copy the code since any small change in the original code can break it.

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 23, 2017, 10:10pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/10 "2017-10-23T22:10:17Z")

</div>

My proposal for the `includeonly` function was to consider only certain kinds of definitions (e.g. functions, structures, macros, constants, etc), and only those that are at the first level of the script (same level as the `main` function). So if a function is defined within a macro, the macro itslef should be included. Same if a function is defined within another function, only the external function could be included.  
I am not an expert so maybe I cannot see all possible difficulties and consequences, but I think such a functionality would be very handy.

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 26, 2017, 9:23pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/11 "2017-10-26T21:23:34Z")

</div>

Ok, as I am probably the only one here thinking that an `includeonly` function would be useful, and I would like to better understand and conform to Julia’s best coding practices, I would like to ask some more information about the following suggestion:

> [@yuyichao](#):
>
> You don’t have to split the file to make part of it reusable though, you can just have a check for a certain global variable.

Could you please provide an example or a documentation reference?  
Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 26, 2017, 9:32pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/12 "2017-10-26T21:32:17Z")

</div>

I’m pretty sure someone has mentioned using it in a script somewhere but I don’t remember what/where it was. But the idea is trivial

You just have

```julia
if !@isdefined(Do_not_run_the_script_please)
    run_the_script()
end

```

And the caller can just do

```julia
Do_not_run_the_script_please = nothing
include("the_script.jl")

```

It’s the same as what you do in python, just not a standard way due to not having file bound to module.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 27, 2017, 7:12am UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/13 "2017-10-27T07:12:56Z")

</div>

Or something like

```julia
if haskey(ENV, "RUNSCRIPT")
    run_the_script()
end

```

which requires no action for _not_ running the script, and just something like  
`ENV["RUNSCRIPT"] = true` for running it.

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 27, 2017, 9:10pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/14 "2017-10-27T21:10:41Z")

</div>

I think that the `julia-config.jl` script behaviour should not change when executed, but only when included by another script. Therefore I think this is the best suggestion:

> [@yuyichao](#):
>
> You just have
> 
> if !@isdefined(Do\_not\_run\_the\_script\_please)  
> run\_the\_script()  
> end
> 
> And the caller can just do
> 
> Do\_not\_run\_the\_script\_please = nothing  
> include(“the\_script.jl”)

So then what if an environment variable was defined (for instance `INCLUDED`) which is usually `false`, and is temporarily set `true` by the `include` function itself?  
The standard way of doing this would then become:

```julia
if !INCLUDED
    run_the_script()
end

```

Even better maybe would be to have function `isincluded()`.

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 29, 2017, 10:34pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/15 "2017-10-29T22:34:48Z")

</div>

After lots of searching and reading, I think now I have a better understanding on how Julia code should be organized in modules to make it reusable.  
I have to say that the [“Modules” section of the official Julia documentation](https://docs.julialang.org/en/latest/manual/modules/) (which I had already read before starting this thread) did not help me very much on this, while I found more useful the [“Modules and packages” section of the “Introducing Julia” Wikibook](https://en.wikibooks.org/wiki/Introducing_Julia/Modules_and_packages).  
What however finally opened my eyes are the following two GitHub threads (I add the links here thinking they may be useful for others in future hitting this discussion):  
[https://github.com/JuliaLang/julia/issues/4600](https://github.com/JuliaLang/julia/issues/4600)  
[https://github.com/JuliaLang/julia/issues/23619](https://github.com/JuliaLang/julia/issues/23619)  
IMHO the Julia documentation should eventually include more insight and examples on this topic, but I understand that some important details are still under discussion, so I guess we should wait for them to be completely defined and implemented.

Now I feel I am starting to think more Julia than Python, so if you agree in order to make the `julia-config.jl` functions reusable in `juliac.jl` I am going to submit a PR with the following edits:

- Take all reusable functions out from the `julia-config.jl` script, and put them in a `JuliaConfig.jl` script.
- In the `JuliaConfig.jl` script, wrap all the code within a module named `JuliaConfig`, and export all reusable functions (`cflags`, `ldflags`, `ldlibs`, `allflags`).
- In the `julia-config.jl` script add the following lines:

```julia
include("JuliaConfig.jl")
using .JuliaConfig

```

- Similarly, in the `juliac.jl` script add the following lines:

```julia
include(joinpath("path_to_JuliaConfig", "JuliaConfig.jl"))
using .JuliaConfig

```

If I understood correctly, this is the current syntax as of Julia v0.6 (please let me know otherwise).

In future, from the two GitHub threads linked above maybe the `include` statement will not be needed anymore, however in this case it is not clear to me how I would set the `"path_to_JuliaConfig"`. I am going to ask this question [there](https://github.com/JuliaLang/julia/issues/23619#issuecomment-340309489).

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 30, 2017, 7:37am UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/16 "2017-10-30T07:37:44Z")

</div>

> [@lucatrv](#):
>
> have to say that the “Modules” section of the official Julia documentation (which I had already read before starting this thread) did not help me very much on this

Please submit a PR with your findings so that we can improve the manual!

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [October 30, 2017, 8:36pm UTC](https://discourse.julialang.org/t/how-to-import-functions-defined-in-a-julia-script-file-without-running-the-script-itslef/6603/17 "2017-10-30T20:36:56Z")

</div>

I’ll do when I have some spare time, but for now I am putting all the time I can spare into [static-julia](https://github.com/JuliaComputing/static-julia).
