# How to execute the function in a different directory?

**URL:** <https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [March 9, 2023, 4:36am UTC](https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783 "2023-03-09T04:36:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![manu\_han](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_han/32/47520_2.png) [@manu\_han](https://discourse.julialang.org/u/manu_han)\
**Post date:** [March 9, 2023, 4:36am UTC](https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783/1 "2023-03-09T04:36:33Z")

</div>

When i execute the function (Background) in a different directory (“User/initialization/Background.jl”) in another function (Hi.Hi\_Hello), there is some error. (although code was simplified for the clarify, it would not be matter)

Hi.Hi\_Hello code

```julia
module Hi
using DelimitedFiles
function Hi_Hello()
include("User/initialization/Background.jl")
Background() 
end
end
using .Hi
Hi.Hi_Hello

```

_Background code_

```julia
function Background()
writedlm("Background.txt", data_BG)
end

```

Error: MethodError: no method matching Background()  
The applicable method may be too new: running in world age 32630, while current world is 32631.  
Closest candidates are:  
Background() at C:\Windows\Application Data\User\initialization\Background.jl:6 (method too new to be called from this world context.)  
Stacktrace:

But, when I execute the same function in the same directory, or command on the terminal, it works.

command on the terminal example:  
**include(“User/initialization/Background.jl”)**  
**Background()**

I don’t know why it do not work, at all.  
If you give me any knowledge, it would be very helpful to me.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 9, 2023, 5:17am UTC](https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783/2 "2023-03-09T05:17:00Z")

</div>

> [@manu\_han](#):
>
> ```julia
> function Hi_Hello()
> include("User/initialization/Background.jl")
> 
> ```

`include()` needs to happen in global scope, not inside a function

---

<div class="post-metadata">

**Author:** ![manu\_han](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_han/32/47520_2.png) [@manu\_han](https://discourse.julialang.org/u/manu_han)\
**Post date:** [March 9, 2023, 7:02am UTC](https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783/3 "2023-03-09T07:02:53Z")

</div>

Could you give me a simple example?  
I found some strange point that the error only happens after executing the file in REPL.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [March 9, 2023, 11:39am UTC](https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783/4 "2023-03-09T11:39:17Z")

</div>

As Jerry says, move the `include` line out of the `function`:

```julia
module Hi
using DelimitedFiles
include("User/initialization/Background.jl")
function Hi_Hello()
    Background() 
end
end

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 9, 2023, 12:02pm UTC](https://discourse.julialang.org/t/how-to-execute-the-function-in-a-different-directory/95783/5 "2023-03-09T12:02:07Z")

</div>

> [@manu\_han](#):
>
> Error: MethodError: no method matching Background()  
> The applicable method may be too new: running in world age 32630, while current world is 32631.

For the record this error has nothing to do with a different directory but with evaluating code which creates a new function inside a running function. A shorter way to demonstrate this is

```julia
julia> function hi()
           @eval hello() = println("hello")
           hello()
       end
hi (generic function with 1 method)

julia> hi()
ERROR: MethodError: no method matching hello()
The applicable method may be too new: running in world age 31319, while current world is 31320.

```

There are some ways to work around this but those who say that you shouldn’t use `include` inside a function are absolutely right.
