Working with multiple files

I’m missing something as far as how to work with multiple files.

I have a directory where I keep two *.jl files, say “core.jl” and “scripts.jl” One has most of the core analytical code, the other has some very project specific scripts. At the top of scripts.jl, I have:

using DataFrames, Distributions, StatsBase, MyModule

where MyModule is in core.jl. If I run scripts.jl without running core.jl first, I get:

LoadError: ArgumentError: Module MyModule not found in current path.

I tried putting the following code at the top of scripts.jl (path edited for brevity):

if length(findin("C:\\MyProject17",LOAD_PATH)) == 0
  push!(LOAD_PATH,"C:\\MyProject17")
end

This had no effect.

If I run core.jl first, everything works fine, so this is more of an annoyance. In the off-chance this is an actual issue, happy to post there, but I figure I am most likely just missing something on how to properly link the files. I’m using Julia 0.6/Atom/Juno if it makes a difference.

include("core.jl") in your script file

Just to further explain this point: if you put include("core.jl") in your script file, then each time you edit and reload the script file, the core routines will also get recompiled. That may or may not be desirable depending on the relative size of the files and how often each needs editing.

It is a rather large module, so it would be ideal to not recompile each time. Also, when I do include("core.jl") I get the following error: .

LoadError: UndefVarError: dropNullsFromDF not defined

Where dropNullsFromDF is an exported function in core.jl and MyModule. I’d rather not write out MyModule.dropNullsFromDF or whatever function I am calling each time I use a function from MyModule.

Solved (sort of). Having include("core.jl") AND using MyModule works, but with a couple of warnings on recompilation

In order for using to work without including the file, it needs to follow the structure of a package and be available in LOAD_PATH.

I see, thank you. Just to be clear, the only way to avoid the warnings is to have a package? Is there any way to do this without setting something up on git?

I’m just looking for a light-weight way to work with multiple files without having to go through the overhead of registering it all on git and its version control (this is a mostly solo project).

You can’t avoid the warning if you changed the module.

Yes, by

I see, thank you.

I added the directory to load path. I’m less concerned about the standard replacing module warning and more concerned about the following:

WARNING: using MyModule.MyModule in module myScript conflicts with an existing identifier.

I’m fine with ignoring it if that’s what i need to do.

Also, I noticed wrapping scripts.jl in a module of its own breaks this approach. Just to be clear, this is what I am running:

module myScript

if length(findin(pwd(),LOAD_PATH)) == 0
  push!(LOAD_PATH,pwd())
end

include("core.jl")
using DataFrames, StatsBase, Distributions, MyModule


println(myMethod())

end

Error:

LoadError: ArgumentError: Module MyModule not found in current path.
Run `Pkg.add("MyModule")` to install the MyModule package.
while loading C:\Users\Clinton\Dropbox\Projects\JuliaTest\scripts.jl, in expression starting on line 11
in require at base\loading.jl:398
in _require at base\loading.jl:428

I think I got it- I put everything into the same module name as in some of the examples at https://docs.julialang.org/en/stable/manual/modules/#modules-1. I think the similarities and differences between the C++ namespace and class construct and the module construct in Julia were throwing me off.

Thank you @yuyichao and @kristoffer.carlsson for the help and patience.

For a module to be found in LOAD_PATH, it needs to be in a file with the same name.
MyModule should be in MyModule.jl

So you could try changing filename core.jl to MyModule.jl, and inserting the following into scripts.jl:

thisdir = dirname(@__FILE__())
any(path -> path==thisdir, LOAD_PATH) || push!(LOAD_PATH, thisdir)
using MyModule

Beautiful! That worked well. Thank you