# Why does code in a package use "using" and "import"?

**URL:** https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500
**Category:** New to Julia
**Created:** [October 16, 2020, 4:24pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500 "2020-10-16T16:24:38Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)
#### Post date: [October 16, 2020, 4:24pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/1 "2020-10-16T16:24:38Z")

</div>

Hi, In looking at how Julia packages are implemented, I see a common idiom that I don’t quite understand. For example, if you look at YaoBase/src/YaoBase.jl at [https://github.com/QuantumBFS/YaoBase.jl/blob/master/src/YaoBase.jl](https://github.com/QuantumBFS/YaoBase.jl/blob/master/src/YaoBase.jl) you see

```julia
using YaoAPI

import YaoAPI: isunitary, isreflexive, iscommute,
    AbstractRegister, AdjointRegister, AbstractBlock,
    PostProcess,
    NotImplementedError, LocationConflictError, QubitMismatchError,
    instruct!, focus!, relax!, nqubits, nremain, nactive, nbatch,
    viewbatch, addbits!, insert_qubits!, measure, measure!,
    occupied_locs, invorder!, partial_tr, select!, ρ, reorder!

```

Looking at the Julia docs at [Modules · The Julia Language](https://docs.julialang.org/en/v1/manual/modules/#Summary-of-module-usage) , I see that the `using YaoAPI` makes all functions accessible (even non-exported ones with YaoAPI.fcnName) and also makes them extensible. As far as I know, all that the long import statement does is allows for use of non-exported functions from YaoAPI without the `YaoAPI.` prefix. So it’s just convenience.

Is that really right or is there more going on here? I’ve started writing my own packages, so I’m wondering what I’m missing, if anything.

Thanks in advance for explanations! – Adam

---

<div class="post-metadata">

### Author: ![wulpuqu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wulpuqu/32/17640_2.png) [@wulpuqu](https://discourse.julialang.org/u/wulpuqu)
#### Post date: [October 16, 2020, 4:30pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/2 "2020-10-16T16:30:18Z")

</div>

Sometimes, it is nice to explicitly specify which function has been imported from a package. It avoids the reader to actually search in the code or other packages where the function is defined.  
Maybe, that’s the reason here.

Otherwise, it might be for overloading functions.

---

<div class="post-metadata">

### Author: ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)
#### Post date: [October 16, 2020, 4:51pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/3 "2020-10-16T16:51:16Z")

</div>

Thanks! That makes sense. – Adam

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 16, 2020, 4:58pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/4 "2020-10-16T16:58:28Z")

</div>

Isn’t it that first, they load all exported functions, and then afterwards, some of the non-exported ones too, for convenience?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 18, 2020, 11:56am UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/5 "2020-10-18T11:56:43Z")

</div>

> [@lyonsquark](#):
>
> I see that the `using YaoAPI` makes all functions accessible (even non-exported ones with YaoAPI.fcnName) and also makes them extensible

I think you misunderstand, you need either

```julia
import Foo: f
f(::SomeType) = ...

```

or

```julia
import Foo # only brings Foo into the namespace, without its exports
Foo.f(::SomeType) = ...

```

to define methods for `Foo.f`.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [October 18, 2020, 12:44pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/7 "2020-10-18T12:44:58Z")

</div>

It’s not quite true. It may look weird, but `using <Module>` indeed make all functions extensible: [Modules · The Julia Language](https://docs.julialang.org/en/v1.4/manual/modules/#Summary-of-module-usage-1)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 18, 2020, 12:59pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/8 "2020-10-18T12:59:17Z")

</div>

You can always define methods if you prefix with the module name — `using Foo` does not make this happen, it just brings `Foo` into the namespace which allows you do do this.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [October 18, 2020, 1:31pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/9 "2020-10-18T13:31:55Z")

</div>

Yes, you are right and docs are misleading. It looks like it should be something like this

In this module we export the `x` and `y` functions (with the keyword `export` ), and also have the non-exported function `p`. There are several different ways to load the Module and its inner functions into the current workspace. Take into account, that irrelevant of the way how you load the Module, `MyModule.x` , `MyModule.y` and `MyModule.p` are always brought into the scope and you can always extend `MyModule.x` , `MyModule.y` and `MyModule.p`

| Import Command | What is brought into scope | Available for method extension |
| --- | --- | --- |
| `using MyModule` | All `export` ed names ( `x` and `y` ) | |
| `using MyModule: x, p` | `x` and `p` | |
| `import MyModule` | | |
| `import MyModule.x, MyModule.p` | `x` and `p` | `x` and `p` |
| `import MyModule: x, p` | `x` and `p` | `x` and `p` |

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 18, 2020, 1:37pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/10 "2020-10-18T13:37:31Z")

</div>

> [@Skoffer](#):
>
> Take into account, that irrelevant of the way how you load the Module, `MyModule.x` , `MyModule.y` and `MyModule.p` are always brought into the scope and you can always extend `MyModule.x` , `MyModule.y` and `MyModule.p`

I think the docs are OK, but this version is not correct, eg simply `using MyModule: x, p` will not work as it does not bring `MyModule` into scope.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [October 18, 2020, 2:02pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/11 "2020-10-18T14:02:43Z")

</div>

Aha, it all clicks together now.  
I was experimenting with modules locally, and include them with `include` command. In this case `MyModule` was loaded to scope and I have had `MyModule.x` etc with all the consequences.

Now, if `MyModule` lives in a separate package, the situation is somewhat different and the docs table is valid. And the difference between `using MyModule` and `using MyModule: x` exists. Here is an example

```julia
julia> using DecisionTree

julia> methods(build_tree)
# 12 methods for generic function "build_tree":

julia> build_tree(x) = x
ERROR: error in method definition: function DecisionTree.build_tree must be explicitly imp
orted to be extended

julia> DecisionTree.build_tree(x) = x

julia> methods(build_tree)
# 13 methods for generic function "build_tree":

```

So, `build_tree` can not be extended on it’s own, but it can be extended with module name.

But if only one name is imported with the `using` then it can’t be extended at all

```julia
julia> using DecisionTree: build_tree

julia> build_tree(x) = x
ERROR: error in method definition: function DecisionTree.build_tree must be explicitly imp
orted to be extended
Stacktrace:
 [1] top-level scope at none:0
 [2] top-level scope at REPL[2]:1

julia> DecisionTree.build_tree(x) = x
ERROR: UndefVarError: DecisionTree not defined
Stacktrace:
 [1] top-level scope at REPL[3]:1

```

To clarify, what I wanted to say in initial post is that both constructions are ok

```julia
import Foo # only brings Foo into the namespace, without its exports
Foo.f(::SomeType) = ...

```

```julia
using Foo # brings Foo into the namespace and its exports
Foo.f(::SomeType) = ...

```

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [October 19, 2020, 6:25am UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/12 "2020-10-19T06:25:31Z")

</div>

Here is short summary of the discussion:

```julia
using Foo
import Foo: bar, baz

```

has the following effects:

1. You can access and extend all `Foo` methods using `Foo.` prefix (this one is coming from `using Foo`)
2. You can use all `Foo` exported methods without using `Foo.` prefix (also from `using Foo`)
3. You can use `bar` and `baz` methods without using `Foo.` prefix (from `import Foo: bar, baz`)
4. You can extend `bar` and `baz` methods without using `Foo.` prefix (from `import Foo: bar, baz`)

Also, you may try to compare it with different combinations of `using`/`import`

```julia
using Foo
using Foo: bar, baz

```

Comparing to the previous case you have the following:

1. You can use all methods in the same way as in previous case, i.e. `Foo.foo1`, `Foo.bar`, `bar`, `baz` and so on.
2. You can extend all methods using `Foo.` prefix as in previous case.
3. You can’t extend `bar`, `baz` without using `Foo.` prefix ← this is the main difference.

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [October 19, 2020, 8:49am UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/13 "2020-10-19T08:49:38Z")

</div>

Not telling anything new, but this way of explaining it helps _me_ understand better:

- About importing and using modules:

- About importing and using objects from a module:

- Additionally, all functions from a given module are always available for method extension _in their module’s scope_. So, if the module `Foo` has been brought into scope of module `Bar` (with `using` or `import`), its functions can be extended in the code of `Bar` referring to them as `Foo.x`, etc.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 19, 2020, 8:52am UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/14 "2020-10-19T08:52:26Z")

</div>

> [@heliosdrm](#):
>
> this way of explaining it helps _me_ understand better

This is how I think about this, too. I think this part of the manual should be revised accordingly.

---

<div class="post-metadata">

### Author: ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)
#### Post date: [October 19, 2020, 1:50pm UTC](https://discourse.julialang.org/t/why-does-code-in-a-package-use-using-and-import/48500/15 "2020-10-19T13:50:07Z")

</div>

Thanks @heliosdrm! That’s a nice succinct summary of `usage` and `import`.
