Almost there
I myself push!()
my custom package library onto LOAD_PATH
in much the same way as you do in your example @sxd190026.
And, despite some of the comments that almost seem to imply otherwise, I don’t believe this is necessarily a bad technique.
In fact, I find it is one of the easiest ways to add your custom library of utilities to new Julia projects. I think the only real drawback is that it doesn’t lend itself quite as well to sharing these Julia environments with other co-workers.
LOAD_PATH
As @skleinbo said:
-
push!(LOAD_PATH, "/abs/path/to/my/package_lib")
essentially adds a package library folder to Julia’s search path. - That means
include()
(file loading mechanism) is unaffected byLOAD_PATH
. - However:
using
&import
(package loading mechanisms) are affected byLOAD_PATH
.
include()
vs (using
or import
)
include("relative/path/to/file.jl")
:
- Loads code from
file.jl
.
import MyAwesomePackage
:
- Loads entire “package”
MyAwesomePackage
.
using MyAwesomePackage
:
- Essentially does the same as
import MyAwesomePackage
- But also makes any symbols
export
-ed fromMyAwesomePackage
directly available in the caller’s scope.
Also note that both using
and import
can be used with already-loaded modules (instead of packages) - but we’ll ignore that complexity for now (I don’t find it to be as useful in typical cases - or this discussion).
So what was missing?
There were really only 2 things missing from your solution:
- You should import your code as a package instead of loading it as a file.
-
using MyAwesomePackage
– instead of –include("file_name.jl)
-
- Packages need to be wrapped in a
module
-end
block.
So, in "/abs/path/to/my/package_lib"
, I suggest you create a test package:
#MySingleFilePackage.jl
module MySingleFilePackage #Module needs to match file/package name
dosomething() = println("Execute all algorithms!")
export dosomething #If you don't want people to prepend call with "MySingleFilePackage."
end #module MySingleFilePackage
And try it out in your script:
#Just try not to run push!() more than once in your scripts:
push!(LOAD_PATH, "/abs/path/to/my/package_lib")
using MySingleFilePackage
dosomething()
What if I want to create a multi-file package?
Then I suggest you follow the structure for full-blown package described by @GunnarFarneback.