Julia Community 🟣

Cover image for What's the difference between using and import in Julia?
Ifihanagbara Olusheye
Ifihanagbara Olusheye

Posted on • Updated on • Originally published at hashnode.ifihan.dev

What's the difference between using and import in Julia?

Julia is a high-level, dynamic programming language with high performance. It is a general-purpose programming language used to write any application. Like many programming languages, Julia allows users to bring external modules and their functions into their current working environment to access and use them. The two main ways to do this in Julia are through the using and import keywords.

While they both achieve the same goal of importing external modules, there are significant differences in how they work and their impact on code organization and performance. Understanding the differences between using and import is important for writing efficient and maintainable code in Julia. In this article, we will explore the differences between using and import in Julia, their advantages and disadvantages, and best practices for using them effectively.

The using keyword in Julia

In Julia, the using keyword is used to load all exported names from a module and bind them to the current namespace. In other words, every variable or function declared in the module can be accessed directly in the current environment. The syntax for the using keyword is:

using ModuleName
Enter fullscreen mode Exit fullscreen mode

The above keyword will load all exported names from ModuleName and make them available in the current environment. For example, if we want to use the LinearAlgebra module in our code, we can use the using keyword as follows:

using LinearAlgebra
Enter fullscreen mode Exit fullscreen mode

After this keyword, we can use any functions directly from the LinearAlgebra module without having to prefix the module name. For example, calculating the dot product of variables x and y using the dot function from the LinearAlgebra module.

using LinearAlgebra

x = [1, 2, 3]
y = [4, 5, 6]

println(dot(x, y)) # 32
Enter fullscreen mode Exit fullscreen mode

The import keyword in Julia

Apart from the using keyword to import modules in Julia, the import keyword can also be used. It is used to selectively import specific functions or variables from a module, and it requires the user to specify the module name before the function or variable name when it is used. In other words, a function or variable not explicitly imported cannot be accessed in the current environment. The syntax for the import keyword is:

import ModuleName: Function1, Function2
Enter fullscreen mode Exit fullscreen mode

The above keyword will only import the specified functions from ModuleName into the current environment. Following the previous example above, if we want to use the dot function from the LinearAlgebra module, we will import it as:

import LinearAlgebra: dot
Enter fullscreen mode Exit fullscreen mode

After this keyword, we can use the dot function directly in our code as follows:

import LinearAlgebra: dot

x = [1, 2, 3]
y = [4, 5, 6]
println(dot(x, y))  # 32
Enter fullscreen mode Exit fullscreen mode

Benefits and Limitations of each keywords

It has been established that the two keywords are used to load up modules into our code. We will be looking at the benefits and limitations of these keywords.

One advantage of using is convenience. It can make the code more concise by making all functions and variables from a module available in the current namespace. This can also make the code easier to read and understand. But a downside to this is that using using to load large modules with many functions and variables can impact performance and memory usage.

Another limitation or disadvantage of using is the potential for naming conflicts. The using keyword can cause naming conflicts if multiple modules define functions or variables with the same name.

On the other hand, using import to import only the necessary functions or variables from a module can help reduce compile times by avoiding the need to compile unnecessary code. But using the import keyword leads to increased typing and possible overhead and complexity.

Conclusion

In this article, we have seen how to import external modules into our Julia program, the benefits, and limitations of using either of the options.

Ultimately, the decision to use the keyword or the using keyword depends on the specific needs of the project and the preferences of the developer. The using keyword may be more suitable for smaller projects where convenience is prioritized, while the import keyword may be more appropriate for larger projects where clarity, selectivity, and better control over memory usage are important.

Top comments (5)

Collapse
 
numbermaniac profile image
numbermaniac

I think there might be a small mistake at the end - if I copy the code in your last code block and run it in a REPL:

import LinearAlgebra: dot

x = [1, 2, 3]
y = [4, 5, 6]
LinearAlgebra.dot(x, y)
Enter fullscreen mode Exit fullscreen mode

Then, in 1.9, I get:

ERROR: UndefVarError: `LinearAlgebra` not defined
Enter fullscreen mode Exit fullscreen mode

It seems the correct way is to do dot(x, y) (without the package name in front) instead.

Collapse
 
ifihan profile image
Ifihanagbara Olusheye

Thank you so much for the observation. I must have mixed it up when copying the code to my article. Thank you once again!

Collapse
 
hungpham3112 profile image
hungpham3112

Nice article. I was a bit confused between those 2 keywords but now I have no more

Collapse
 
ifihan profile image
Ifihanagbara Olusheye

I'm glad to hear this!