@Amro:
To start off, I should first mention my agreement with @stevengj 's previous post. It is highly likely that you would benefit overall by creating an actual package to house your new code.
Having said that, I noticed there being much confusion regarding the differences between include
and using
/import
. I think it is worth explaining these differences so you can avoid pitfalls like the one your issue has hilighted.
include()
: What does it do?
Applying terminology typical programmers seem more familiar with: we could almost say that:
→ include("some_file_with_code.jl")
causes Julia to “compile” some_file_with_code.jl
— inline with said call (i.e. at the location/time the compiler reaches your call to include()
).
Sidenote:
However, if I am to use more accurate terminology, I believe you should say that the Julia parser “parses” that file, and performs code lowering in order to store various symbols, modules, functions, etc. into a complex tree of data structures… Because what is technically known as the “compilation process” actually happens at a later time.
Point being, the code you include()
is available for use immediately after the call. Upon inclu
-sion, Julia generates the appropriate symbols, and makes them available to your current scope…
And if you are wondering, calling:
julia> include("some_file_with_code.jl")
from the Julia prompt makes the newly “lowered” code available in the module named >>Main
<<.
using
: What does it do?
In the case where we are dealing with pure modules (not full-blown packages), using SomeModuleSymbol
gets Julia to import a symbol (here: your module’s name) into the current namespace.
Note that it would also automatically import any symbols that have been marked for exporting using “export
statements” (had you included any inside your module).
“Ok. So what’s wrong with my code?” (paraphrasing)
First off, when you: include("name_module.jl")
, Julia does the whole code-lowering thing, and makes “name_module
” available in your namespace.
Then, when you call:
using .name_module
Julia tries to re-import “name_module
” that is already available in said namespace.
So what do I do to fix this?
There is simply no need for the “using .name_module
” command in your example.
Instead, you can immediately start using the module that was made available by calling “include("name_module.jl")
”, as done here:
include("name_module.jl");
#using .name_module #Nope: Will re-import already existing symbol
#Just start using the module that is now available:
name_module.run_something_inside_module()
# rest of the code