Hello!
I am a new Julia user, using v1.10, and am struggling a bit to fully understand how to get multiple code files to work together. Specifically, I am finding that editing files can throw errors, and my using
statements do not always import necessary code. The documentation seems pretty confusing to me, and I haven’t found julia examples that use multiple files like this, so I thought I would ask for help here.
I have a filesystem with three files. The system looks like the following.
Module1.jl
ObjectFolder
⤷Object1.jl
RunnerFolder
⤷Runner1.jl
Let’s go through each of the files now.
Object1.jl is a file that simply contains a struct definition. Later, I might add some functions that use this, but for now I simply care about defining a new data type:
struct Object1
variable1::Vector{Int64}
end
Module1.jl is a file containing a simple module definition. Currently, all it does is include Object1.jl:
module Module1
include("ObjectFolder/Object1.jl");
end
Finally, Runner1.jl is intended on using the code in Module1 inside a coherent script. The concept is that all my helper code is in the module, and then my Runner actually uses the code:
using Module1
Alice = [1,2,3];
print(Alice);
Bob = Object1(Alice);
print(Bob)
For the purposes of this post, I have changed the name of the files, but the contents are correct. I have added the parent folder containing Module1.jl and every subfolder to the LOAD_PATH so that it is recognized by the “using” statement correctly.
When I run this, I get the following error:
[1, 2, 3]ERROR: UndefVarError: `Object1` not defined
Stacktrace:
[1] top-level scope
@ FILEPATH\Runner1.jl:4
So, I know that the “using” statement does not throw an error, because Alice is successfully printed. However, the “Module1” module is not being imported by the using
statement, as the error shows. That, or the module is not actually functioning as I would expect, and is not importing the struct code from Object1.jl.
I have a few questions, for anyone who would be willing to help me out here:
- Why is the Object1.jl code not being imported by the module? Am I not understanding how modules are supposed to function?
- How do I get two code files to work together like this? I am explicitly trying to avoid defining all of my code in one file, because I will soon have to create a project that has too much code to keep track of in one place.
- Are there any “Best Practices” for organizing code in multiple files like this? How would you approach this problem?