Reputation: 2310
I want to help Julia find my .jl file by modifying the LOAD_PATH
variable:
julia> readdir()
1-element Array{String,1}:
"test.jl"
shell> cmd /c type test.jl
# Test.jl
module Test
export f
f() = println("hi")
end
julia> push!(LOAD_PATH,pwd());
julia> import Test
ERROR: ArgumentError: Module Test not found in current path.
Run `Pkg.add("Test")` to install the Test package.
in require(::Symbol) at .\loading.jl:365
The first call to readdir()
proves that I have a file called test.jl in my current directory. The following shell call shows that this file contains a module called Test. The next call to push!(LOAD_PATH,pwd());
puts the current directory in LOAD_PATH
. But even with the current directory in LOAD_PATH
, Julia still can't find the Test
module in test.jl.
What's wrong?
Upvotes: 4
Views: 5241
Reputation: 8566
The error was talking about something concerning require
. As the doc says:
Given the statement using
Foo
, the system looks forFoo
withinMain
. If the module does not exist, the system attempts torequire("Foo")
, which typically results in loading code from an installed package. ...require
is case-sensitive on all platforms, including those with case-insensitive filesystems like macOS and Windows.
and the reason is clear: require
couldn't find a file named Test
in LOAD_PATH
. So we need to make the file name matching the module name, but this is just a convention, not a mandatory rule. What will happen if someone mistakenly runs using test
?
julia> push!(LOAD_PATH,pwd())
julia> using test
WARNING: requiring "test" in module "Main" did not define a corresponding module.
julia> whos()
Base 34427 KB Module
Core 12386 KB Module
Main 41296 KB Module
Test 1837 bytes Module
The result shows that we've loaded the file test.jl
and the module(Test
) in it, but not actually using/import
the module. This is a respected behavior since we used a wrong module name, which is also the reason why julia complained in the warning. In this case, using test
is equivalent to include("test.jl")
, but I highly recommend you to follow the convention and do not use this behavior.
BTW, require
became generally case-sensitive after this PR. A side-effect is your LOAD_PATH
should also be case-sensitive, this will be fixed by this PR in julia-0.6.
Upvotes: 5