What's the difference between using and import in Julia?
Modules can be loaded in Julia using either the using statements or the import statements. While they are often used interchangeably, these statements are different in terms of function extension. In this Answer, we'll discuss the difference between using and import statements.
The using statement
The using statement is conventionally used to import a module. When the using statement is used, the module name is imported in the namespace and the exported variables are available in the global scope.
Example
Note: All code examples in this Answer have been implemented in Julia 1.8.1.
Let's look at the following code example to understand the using statement better:
# Define a dummy variablemodule foof = 3g = 4export fendusing .fooprintln("f: ", f)println("g: $(foo.g)")
In this code:
Lines 2–6: A module
foois defined which defines variablesf=3andg=4and exportsf.Line 8: The module
foois loaded in the main scope using theusingstatement.Line 9:
fis loaded as a global variable because ofexportinfoo, and its value is printed for verification.Line 10: Since
gis not exported byfoo, it needs to be used with its module name.
Note: If you try to export a variable of the module that's already declared before the module is loaded, Julia will issue a warning and will stick to the prior declaration.
The import statement
When a module is loaded using the import statement, only the module name is loaded in the namespace. All variables and functions need to be loaded using the module name.
Example
We'll use the same example as before, with the key difference of loading the module using the import statement. Let's look at the following code example to understand the import statement better:
# Define a dummy variablemodule foof = 3g = 4export fendimport .fooprintln("f: ", foo.f)println("g: $(foo.g)")
In this code:
Lines 2–6: A module
foois defined which defines variablesf=3andg=4and exportsf.Line 8: The module
foois loaded in the main scope using theimportstatement.Line 9: Even when
fis exported byfoo, it can only be used with the module name.Line 10:
gis being loaded with its module name.
Method extension
Apart from the scope of the exported variables, one key difference between the using statement and the import statement is that a module method can only be extended using the import statement. Let's have a look at the following code example:
module randomModulerandomFunction() = "This is a nice function in a nice module."export randomFunctionendusing .randomModule: randomFunctionrandomFunction() = "This is a nice function."println(randomFunction())
Here, trying to extend the randomModule.randomFunction() using the using statement causes an error. This can be fixed by using the import statement.
module randomModulerandomFunction() = "This is a nice function in a nice module."export randomFunctionendimport .randomModule: randomFunctionrandomFunction() = "This is a nice function outside the nice module."println(randomFunction())
Free Resources