Is there ever a reason to call Base.require
directly, or is it a function that
is used only internally by import
/using
, and I should never call it directly
and forget it exists?
Since the argument to require
is a module name, not a filename, which source file is loaded? How does it make the connection between the module name and the file name? The documentation says “When searching for files, require first looks for package code under Pkg.dir(), then tries paths in the global array LOAD_PATH”, but that seems somewhat ambiguous. By extension, since require
provides the implementation for import
/using
, my question is really about how modules are found in those instances, too.
The Julia documentation on Modules says “Files and file names are mostly unrelated to modules; modules are associated only with module expressions”. However, some experimentation seems to indicate that there is a pretty direct link: using MyModule
has worked for me in exactly two cases:
-
There is a file
MyModule/src/MyModule.jl
inPkg.dir()
that defines a moduleMyModule
If the file
MyModule/src/MyModule.jl
defines a different module thanMyModule
, that module is actually imported (but not its exported names), but Julia shows a messageWARNING: requiring "MyModule" in module "Main" did not define a corresponding module.
If
MyModule/src/MyModule.jl
defines one or more modules in addition
toMyModule
(e.g.MyModule2
andMyModule3
), these modules are imported alongside “MyModule” (again, without their exported names). One can subsequently dousing
MyModule2to get the exported names in
MyModule2, but there seems to be no way to get
MyModule2without
MyModule`. So why would one define more than one module per file? (and arguably: why would this even be allowed? -
There is a file
MyModule.jl
in any directory inLOADS_PATHS
. The behavior is pretty much the same as for the package (the file must define the moduleMyModule
and it behaves in the same way regarding other/additional modules defined in the same file.
Are these empirical rules I’m observing the official behavior? Is this spelled
out somewhere in the documentation, in a place I didn’t find?
So in essence it looks to me like Julia uses “Packages” as a way to distribute exactly one module (possibly with submodules, but submodules don’t seem to be encouraged, or at least I haven’t found any of the major packages that use them.
One more question: Is there a way to have multiple directories in Pkg.dir()
?