# Using vs include

**URL:** <https://discourse.julialang.org/t/using-vs-include/109036>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 20, 2024, 5:18am UTC](https://discourse.julialang.org/t/using-vs-include/109036 "2024-01-20T05:18:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 20, 2024, 5:18am UTC](https://discourse.julialang.org/t/using-vs-include/109036/1 "2024-01-20T05:18:05Z")

</div>

```julia
include("./basis.jl")
include("./ham.jl")
#using basis
#using ham

```

This is the header of a julia script. In the same directory, there are two files basis.jl and ham.jl.

**To include is just to copy and paste the content of the files in the current script** , right?

I find that if the include is commented out, while the using is uncommented, I get an error

```julia
ERROR: ArgumentError: Package basis not found in current path.

```

ps. the other files contain only functions, no module definition

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [January 20, 2024, 6:07am UTC](https://discourse.julialang.org/t/using-vs-include/109036/2 "2024-01-20T06:07:10Z")

</div>

Assuming that the files contain a module with the same name (i.e. `module basis ... end`), then you need to do `using .basis`.

`using` with no period looks at the environment for installed packaged. The leading period indicates that you are referring to a module defined in the current module.

If you do not have a module with a matching name to the file, then it doesn’t work because copy and pasting the contents of the file does not create a module with a matching name.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [January 20, 2024, 3:34pm UTC](https://discourse.julialang.org/t/using-vs-include/109036/3 "2024-01-20T15:34:12Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> **To include is just to copy and paste the content of the files in the current script** , right?

No, it’s not. In most cases it behaves like that, but it’s not what it does. Instead, [as the documentation says](https://docs.julialang.org/en/v1/base/base/#Base.include):

> ```julia
> Base.include([mapexpr::Function,] m::Module, path::AbstractString)
> 
> ```
> 
> Evaluate the contents of the input source file in the global scope of module `m`

with the special case that `include(file)` uses the _current_ module for `m`. So it’s more like “copy and paste the content of the file at the top of the module”. It’s the reason you can’t just `include` a file inside a function, which would work if it was literally just copying in the code.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 20, 2024, 4:02pm UTC](https://discourse.julialang.org/t/using-vs-include/109036/4 "2024-01-20T16:02:36Z")

</div>

It’s common for beginners or people unfamiliar with modules to confuse `import`/`using` and `include` or wonder why there’s 2 things for code loading. `include` evaluates source code from a file, while the primary purpose of `import`/`using` is to share names between 2 modules. Imports only load (often cached and precompiled) code from _packages_ directly and _extensions_ indirectly, which are organized into modules. Imports and `include` are really 2 independent actions.

I may not have to say this, but another common tip is to avoid multiple `include`s of a module-defining source file because it would create multiple independent runtime copies of the module, which is almost always unintended and buggy. As you can probably tell from the good information in the thread, you would `include` the file _once_ to load the module, then many other modules can each `import`/`using` names from that one module. Of course, if the module was a package, then you wouldn’t need to `include` at all, and the code loading only occurs on the first import.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [January 20, 2024, 8:02pm UTC](https://discourse.julialang.org/t/using-vs-include/109036/5 "2024-01-20T20:02:42Z")

</div>

See also : [11 - Developing Julia packages - Julia language: a concise tutorial](https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/11-developing-julia-packages)
