Hi. I have a folder with four .jl file. One is the module file with name myModule.jl, the second one type.jl is the file with all the type definitions, the third one gen_csv file is the file of function definitions, the forth one example.jl is the main file I’d like to test. In the module myModule.jl, I export myFunction.
Now, in the example.jl, I put
include(“myModule.jl”)
myFunction(myParameter)
this function myFunction is defined in the file gen_csv.jl.
But I got an error, saying that
UndefVarError: myFunction not defined.
I was deeply confused. :((( Any help please?
The other concern is, I’m using Atom to run the file. After keeping running it, I got an error, saying that "warning: replacing module myModule. I do not know why. Could anyone please help me?
Could you please provide a complete Minimal (Non-)Working Example of what you’re trying to do? And also quote your code?
That being said, I think what you are missing is a statement which imports your module (cf. Modules, such as
using MyModule
When you only include("myModule.jl"), you make Julia load all the code it contains (and therefore it is aware of the existence of the module). But you have to also “import” the module, i.e. tell Julia that you actually want (some of) what it defines to be brought into your current scope.
module MyModule
export add1
# Put your function here or in a separate file as you'd like:
# this is not what's causing the problem here...
function add1(x)
x+1
end
end
main.jl:
push!(LOAD_PATH,"/some/path/")
using MyModule
a = add1(3)
println(a)
However, instead of manually fiddling with your LOAD_PATH, you would probably be better off creating your own project. When you activate a project, you don’t have to tell Julia where modules are: the standard files layout helps it find your source code.