How to execute the function in a different directory?

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

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

Background code

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.

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

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

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

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

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> 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.

2 Likes