# Import vs using (v0.7)

**URL:** <https://discourse.julialang.org/t/import-vs-using-v0-7/11753>\
**Category:** General Usage\
**Created:** [June 18, 2018, 11:46am UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753 "2018-06-18T11:46:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 18, 2018, 11:46am UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753/1 "2018-06-18T11:46:52Z")

</div>

I was reading the `Pkg` documentation for Julia v0.7 and I noticed that the examples use `import`. I was wondering if this represents idiomatic Julia and if `import` is recommended over `using`.

Having clear what `using` and `import` do, is it better to consistently use `import` – thinking that it doesn’t add the modules to be searched, so it provides better performance?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 18, 2018, 12:19pm UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753/2 "2018-06-18T12:19:57Z")

</div>

There is no performance difference, the difference is if the exported variables are brought into scope. `using` is nice for interactive code but for library code it is in my opinion more descriptive (at least when the code size starts to get larger) to use `import`.

Compare e.g.

```julia
using A, B, C # A exports p, B exports y, C exports z

foo(x) = p(x) + y(x) + z(x)

```

vs

```julia
import A, B, C

foo(x) = A.p(x) + B.y(x) + C.z(x)

```

or

```julia
import A.p, B.y, C.z

foo(x) = p(x) + y(x) + z(x)

```

In both the latter examples, it is easy to see where `p`, `y` and `z` comes from. In the first one you have to either run `using A, B, C` in the REPL and just see where they are defined, or search through the source code of `A`, `B`, and `C`.

---

<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:** [June 18, 2018, 12:30pm UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753/3 "2018-06-18T12:30:36Z")

</div>

> [@kristoffer.carlsson](#):
>
> In both the latter examples, it is easy to see where `p` , `y` and `z` comes from.

I agree, but unless I want to define new methods, I prefer `using A: p` etc.

This is my “internal style guide”:

1. `using Foo` for interactive development, or (rarely) importing extensive namespaces in packages.
2. `import Foo` when I want to keep my namespace clean, use qualified `Foo.bar`,
3. `using Foo: bar, baz` in packages,
4. `import Foo: bar, baz` in packages when I define many methods for them (otherwise `import Foo`, `Foo.bar(::Stuff) = ...`).

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [June 18, 2018, 12:42pm UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753/4 "2018-06-18T12:42:58Z")

</div>

> [@Tamas\_Papp](#):
>
> I agree, but unless I want to define new methods, I prefer `using A: p` etc.

Yes, I also use `import` only for functions I want to overload.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 18, 2018, 7:42pm UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753/5 "2018-06-18T19:42:01Z")

</div>

Thank you very much, that’s good to know. Indeed, in library development I prefer fully qualified names – while for user facing APIs I prefer shorter names.

If I don’t need to extend methods I routinely go with `using`. I was considering changing that to `import` statements, hoping it would provide a performance gain. It’s good to know that there is none so I won’t bother doing it.

---

<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:** [July 24, 2018, 8:43am UTC](https://discourse.julialang.org/t/import-vs-using-v0-7/11753/6 "2018-07-24T08:43:05Z")

</div>

Revisiting this topic, is it possible to combine

```julia
import SomePackage
using SomePackage: f

```

into a single line? Note that AFAIK in `v0.7`, `using` _per se_ does not bring the package name into the namespace.

```julia
using SomePackage: SomePackage, f

```

is now deprecated.
