How to import custom module from file without include

I made a custom module in a file database.jl called DecisionNode but I cannot seem to find a way to import the DecisionNode module without using the include function, which I do not want to do since I will be using the DecisionNode module in multiple files.

Is there a way to import this custom module without turning that module into a package and then importing that package?

1 Like

You cannot avoid using include here. See the manual for more information on how Julia projects/packages are typically organized:

https://docs.julialang.org/en/v1/manual/modules/

Note that some people in the community (including myself) are considering this an unsatisfactory state of affairs and this has been the topic of many passionate discussions:

1 Like

Thank you for your answer! It is surprising that a better solution has not been implemented yet.

1 Like

Confusion

@Jack_N: The frustration/dislike of how “include” works stems from people confusing how Julia uses include/import/using vs how other languages use seemingly similar commands.

Question

Short answer

  • include() does not “import” modules within a file.
  • It is probably better to say that julia “compiles” (to a certain extent) the code “inside” whatever module calls the include() command.
  • It is not the same as Python’s “import statement” (likely why people get confused with it).
  • It is also different from C/C++'s use of #include - which is typically only for header files.
  • C/C++ combines compiled code from multiple .c/.cpp files together using a linker (not the #include directive).

In Python:

import TopModule.FooModule.BarModule
  • Parses/“compiles” file TopModule/FooModule/BarModule.py (if not already done)
  • Makes contents available through symbol BarModule.

In Julia:

  • You should include() all your files once in some way (ex: in a master file - ex: src/TopModule.jl).
  • Get a reference to BarModule symbol through [TopModule.]FooModule.BarModule.
  • See example below
#src/TopModule.jl:
#------------------------------------------------------------------
module TopModule
    #These modules could be stored in other files & `include()`-ed here, if desired
    module FooModule
        module BarModule
            foo(x) = 3x
        end #BarModule
    end #FooModule

#Add/"compile" contents of Some_other_file.jl:
include("Some_other_file.jl") #Relative to current file (src/TopModule.jl)

end #TopModule


#src/Some_other_file.jl
#------------------------------------------------------------------
#include() statement in `TopModule.py` "compiles" contents of this file INSIDE module: `TopModule`!

import FooModule.BarModule #Get access to BarModule symbol
#alternative way to get `BarModule` symbol:
using FooModule.BarModule #Also imports any symbol that might be "export"ed by BarModule .

FooModule.f(3) #Call some function in FooModule
3 Likes

The include solution is not something that needs fixing. @MA_Laforge says it very well above. The misunderstanding of the organization of the code is what causes the “problem”. Essentially, include gives the compiler a chance to “see” the code to be compiled. The user then refers to this compiled code through a module (possibly the Main module when working in the repl).

Unlike in Python, a Julia file is not a module (the perks: (1) 1 module can include multiple files, (2) 1 file can define multiple modules). If you don’t make a module exist by includeing the file containing its module ... end code or by generateing a package, how would you import it?

1 Like