# How to canonically load and run user-provided functions from a file?

**URL:** https://discourse.julialang.org/t/how-to-canonically-load-and-run-user-provided-functions-from-a-file/103180
**Category:** General Usage
**Created:** [August 25, 2023, 8:09am UTC](https://discourse.julialang.org/t/how-to-canonically-load-and-run-user-provided-functions-from-a-file/103180 "2023-08-25T08:09:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)
#### Post date: [August 25, 2023, 8:09am UTC](https://discourse.julialang.org/t/how-to-canonically-load-and-run-user-provided-functions-from-a-file/103180/1 "2023-08-25T08:09:23Z")

</div>

Let’s consider a Julia package `OurPackage.jl` that creates a long-running process. In this process, from time to time we want to load a user-provided file and execute a function implemented in it. For example, the user would provide files `setup1.jl`, `setup2.jl`, each with a function `init_setup()`, and then trigger loading them, in which case the file should be read and then the latest `init_setup` is executed.

At the moment, the relevant code we use looks something like

```julia
Base.include(Main, filename)
state = Base.invokelatest(Main.init_setup)

```

Is the best way to achieve our goal or is there is another, more canonical approach?

One downside is, for example, that the user files typically require to load a package `OtherPackage.jl`. If include this in `Main`, we need to make sure that the `Project.toml` already has OtherPackage.jl installed.

On the other hand, we could call `Base.include(OurPackage, filename)` and just have `OtherPackage.jl` as a dependency in it. However, in that case there is a risk that the user defines auxiliary variables which will overwrite something we have defined in `OurPackage.jl`.

A third option could be to ask the users to always wrap their files in a module, e.g., `module UserModule .... end`, and we’d change the code above then to

```julia
Base.include(OurPackage, filename)
state = Base.invokelatest(OurPackage.UserModule.init_setup)

```

Has anyone grappled with such a use caes before and can give us some pointers as to which is the “cleanest” approach here?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [August 25, 2023, 10:57am UTC](https://discourse.julialang.org/t/how-to-canonically-load-and-run-user-provided-functions-from-a-file/103180/2 "2023-08-25T10:57:27Z")

</div>

Does the function have to execute within the long running process? Could you instead start a new Julia process (via Distributed) to activate the user environment and run that function?

---

<div class="post-metadata">

### Author: ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)
#### Post date: [August 26, 2023, 6:06am UTC](https://discourse.julialang.org/t/how-to-canonically-load-and-run-user-provided-functions-from-a-file/103180/3 "2023-08-26T06:06:39Z")

</div>

> [@mkitti](#):
>
> Does the function have to execute within the long running process?

Yes.

> [@mkitti](#):
>
> Could you instead start a new Julia process (via Distributed) to activate the user environment and run that function?

No. This is - potentially - running in an MPI parallel environment, so starting new processes is out of the question.
