# Difference between \`include\` and \`using\`

**URL:** <https://discourse.julialang.org/t/difference-between-include-and-using/87795>\
**Category:** General Usage\
**Tags:** oxygenjl\
**Created:** [September 26, 2022, 12:11am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795 "2022-09-26T00:11:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 26, 2022, 12:11am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/1 "2022-09-26T00:11:40Z")

</div>

When using [Oxygen.jl](https://github.com/ndortega/Oxygen.jl), I noticed an unexpected difference between `using` and `include`. I created a simple project using a slightly modified version of the example code in the readme.

```julia
(@v1.8) pkg> generate OxygenExample
  Generating project OxygenExample:
    OxygenExample/Project.toml
    OxygenExample/src/OxygenExample.jl

shell> cd OxygenExample/
/tmp/OxygenExample

(@v1.8) pkg> activate .
  Activating project at `/tmp/OxygenExample`

(OxygenExample) pkg> add Oxygen HTTP
<snip>
Precompiling project...
  6 dependencies successfully precompiled in 6 seconds. 15 already precompiled.

```

src/OxygenExample.jl

```julia
module OxygenExample
using Oxygen
using HTTP

@get "/greet" function(req::HTTP.Request)
    return "hello world!"
end

function runserver()
    serve()
end

end # module OxygenExample

```

This doesn’t work, when I fetch `/greet`, I always get a 404:

```julia
julia> using OxygenExample

julia> OxygenExample.serve(port=8000)
   ____
  / __\_____  __________  ____
 / / / / |/_/ / / / __`/ _ \/__ \
/ /_/ /> </ /_/ / /_/ / __/ / / /
\ ____/_/|_|\__ , /\ __, /\___ /_/ /_/
          / ____//____ /

[ Info: ✅ Started server: http://127.0.0.1:8000
[ Info: 📖 Documentation: http://127.0.0.1:8000/docs
[ Info: 2022-09-25T17:00:47-0700 - 127.0.0.1:45300 - "GET /greet HTTP/1.1" 404
[ Info: 2022-09-25T17:00:48-0700 - 127.0.0.1:45300 - "GET /favicon.ico HTTP/1.1" 404

```

But after starting a new session `julia --project` in the project directory, this does work:

```julia
julia> include("src/OxygenExample.jl")
Main.OxygenExample

julia> using .OxygenExample

julia> OxygenExample.serve(port=8000)
   ____
  / __\_____  __________  ____
 / / / / |/_/ / / / __`/ _ \/__ \
/ /_/ /> </ /_/ / /_/ / __/ / / /
\ ____/_/|_|\__ , /\ __, /\___ /_/ /_/
          / ____//____ /

[ Info: ✅ Started server: http://127.0.0.1:8000
[ Info: 📖 Documentation: http://127.0.0.1:8000/docs
[ Info: 2022-09-25T17:07:57-0700 - 127.0.0.1:35256 - "GET /greet HTTP/1.1" 200

```

Why is the include necessary? What happened here?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 26, 2022, 12:40am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/2 "2022-09-26T00:40:18Z")

</div>

When you use `add` you are adding a snapshot of the package at the time you did that. The alternative is to use `dev` which will load whatever is currently located there.

You should generally not use `include` outside of a package module. You lose most of the benefits of a package such as precompilation caching.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 26, 2022, 12:42am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/3 "2022-09-26T00:42:42Z")

</div>

Yeah, that is one of many reasons this example bothers me. It only works with the include.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 26, 2022, 4:03am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/4 "2022-09-26T04:03:09Z")

</div>

> [@contradict](#):
>
> julia --project

This should be `julia --project=.` if you are in the directory. Is that what you did?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [September 26, 2022, 6:02am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/5 "2022-09-26T06:02:54Z")

</div>

If you have an existing environment in the folder `--project` seems to do the same thing as `--project=.` for me at least. Though if there isn’t any it activates the default. So in this case it seems like there should definitely be an environment so I don’t think that would be the reason.

With that said I get the same results as OP, not really sure why this is happening.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 26, 2022, 6:56am UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/6 "2022-09-26T06:56:14Z")

</div>

I misunderstood the problem, but I see what the issue is now.

In your example above, you invoked `@get` at the top level of the `module OxygenExample`. The problem is that this is only executed during precompilation. It is not executed when loading the module via `using OxygenExample`.

To have it execute when loading the module, move the code into ` __init__ `:

```julia
module OxygenExample
using Oxygen
using HTTP

function __init__ ()
    @get "/greet" function(req::HTTP.Request)
        return "hello world!"
    end
end

function runserver()
    serve()
end

end # module OxygenExample

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 26, 2022, 3:17pm UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/7 "2022-09-26T15:17:17Z")

</div>

That makes sense, thank you.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 26, 2022, 6:01pm UTC](https://discourse.julialang.org/t/difference-between-include-and-using/87795/8 "2022-09-26T18:01:52Z")

</div>

I agree this is a bit weird and under documented. I created an Oxygen.jl issue:

> <https://github.com/ndortega/Oxygen.jl/issues/73>
>
> On the forums, I encountered some confusion about a \`@get\` route at the top leve…l of a package module:
> https://discourse.julialang.org/t/difference-between-include-and-using/87795
> 
> The problem with putting the routes at the top level of the module is that this is only invoked during precompilation. It is not invoked when the module is loaded.
> 
> For the routes to be loaded at module initialization, the routes should be declared from \`\_\_init\_\_\` or some function called from \`\_\_init\_\_()\`:
> 
> \`\`\`julia
> module OxygenExample
> using Oxygen
> using HTTP
> 
> 
> function runserver()
> serve()
> end
> 
> function \_\_init\_\_()
> @get "/greet" function(req::HTTP.Request)
> return "hello world!"
> end
> # routes may also be contained in src/main.jl
> include(joinpath(@\_\_DIR\_\_,"main.jl"))
> end
> 
> end # module OxygenExample
> \`\`\`
