100

Question: What is the difference between using and import in Julia when I'm building my own module?

My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.

So, how wrong am I?

1
  • 1
    Already the fact that there is a question about this with more than 95 upvotes, indicates that we should remove either using or import. Especially because either can be used to do what the other does (restrict scope and extend methods), when used appropriately, as the answers below show... Commented Oct 17, 2024 at 7:04

3 Answers 3

80

The Julia Modules documentation states:

The import keyword [...] only operates on a single name at a time. It does not add modules to be searched the way using does. import also differs from using in that functions must be imported using import to be extended with new methods. [...] Functions whose names are only visible via using cannot be extended.

(Emphasis mine.)

For example, you can use import to add methods to Base.show to pretty-print your own types, but not with using.

There is also importall that imports all exported names of a module.

(This answer refers to Julia 0.6; the documentation was reworded for 1.0.)

Sign up to request clarification or add additional context in comments.

3 Comments

Very clear thank you (+1+Tick). I actually read that exact passage before asking the question, but clearly didn't decipher the meaning. Your emphasis was very helpful.
Could I say using is restrictive than import since it forbids the name to be extended? Or maybe there is something using could do but import could not.
What does the documentation mean when it says "Functions whose names are only visible via using cannot be extended"? Specifically, what does "visible" mean in the context of function names?
74

The documentation (updated link for Julia 1) about this is excellent. Here's the excerpt which I find to be the most succinct summary:

(a demo module to make the examples below specific)

module MyModule

export x, y

x() = "x"
y() = "y"
p() = "p"

end

(this is a table in the documentation, but StackOverflow still won't add support for tables so... reformatted)

Command

  • using MyModule
    • in-scope: All exported names (x and y) as well as names MyModule.x, MyModule.y, and MyModule.p
    • extensible: MyModule.x, MyModule.y, and MyModule.p
  • using MyModule: x, p
    • in-scope: x and p
    • extensible: (nothing)
  • import MyModule
    • in-scope: MyModule.x, MyModule.y, and MyModule.p
    • extensible: MyModule.x, MyModule.y, and MyModule.p
  • import MyModule.x, MyModule.p
    • in-scope: x and p
    • extensible: x and p
  • import MyModule: x, p
    • in-scope: x and p
    • extensible: x and p

4 Comments

My mind is blown... I hadn't realized that nowadays, using allows import for extension.
Wow, I am voting for removing either using or import. Seems overly complicated to me.
What does extensible mean in this context? I suspect this is a Julia language specific thing... I think I can guess what it means, but it would be nice to have certainty
I'm now able to answer my own question. Here's an example. Say you define a struct MyType. If you want to write that to a CSV file, you would extend the CSV.write function. You do it by writing function CSV.write(..., my_type::MyType) ... end inside one of your own module(s)/file(s)
7

A summary of the main difference, in a way that I find easy to remember:

  1. using NiceStuff allows usage access to exported names without the module qualifier, which import NiceStuff doesn't; and
  2. import NiceStuff: nice allows extension access (adding methods) to the specified function without the module qualifier, which using NiceStuff: nice doesn't.

And a minor difference:
X as Y syntax is allowed for individual identifiers with both using and import (using Random: randstring as rstr, import Random: randstring as rstr) but for the module name itself, import Random as Rnd is allowed while using Random as Rnd is an error.

Some other points I found useful from the Modules docs page

using ModuleName is the only form for which export lists matter at all.

import NiceStuff is equivalent to using NiceStuff: NiceStuff.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.