# Simple example package to imitate

**URL:** <https://discourse.julialang.org/t/simple-example-package-to-imitate/18224>\
**Category:** New to Julia\
**Created:** [December 2, 2018, 7:29pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224 "2018-12-02T19:29:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seanny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seanny123/32/5271_2.png) [@Seanny123](https://discourse.julialang.org/u/Seanny123)\
**Post date:** [December 2, 2018, 7:29pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/1 "2018-12-02T19:29:08Z")

</div>

Although I’ve found coming to Julia from Python to be straightforward and even delightful sometimes. However, as I’m scaling up my program (a Julia implementation of the UCR Dynamic Time Warping Suite of Optimisations), I’m [hitting a few problems](https://stackoverflow.com/q/53583699/1079075).

Are there any relatively small packages I could imitate the structure of?

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [December 2, 2018, 7:37pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/2 "2018-12-02T19:37:13Z")

</div>

[GeoIP.jl](https://github.com/JuliaWeb/GeoIP.jl) is pretty simple though I don’t know whether it’s what you’re looking for.

---

<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:** [December 2, 2018, 7:46pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/3 "2018-12-02T19:46:01Z")

</div>

The problem in the stackoverflow question is that you have managed to create two `Query` types. One inside of `Structs` (which would be `Structs.Query`) and one inside of `Utils` (which would be `Utils.Structs.Query`).  
Remember that `include` is pretty much copy and pasting the code in place so if you include a file twice, it will get executed twice.

We can look at what type `dist` is defined on:

```julia
julia> methods(dist)
# 1 method for generic function "dist":
[1] dist(x::Main.Utils.Structs.Query, y::Main.Utils.Structs.Query) in Main.Utils at utils.jl:9

```

There is no way for Julia to know that `Utils.Struct.Query` is the same as `Query`. Rewriting your file to use the same `Query` struct:

```julia
include("utils.jl")
using .Utils: dist

a = Utils.Query("a", 1)
b = Utils.Query("b", -1)

println(dist(a, b))

```

and it works.

---

<div class="post-metadata">

**Author:** ![Seanny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seanny123/32/5271_2.png) [@Seanny123](https://discourse.julialang.org/u/Seanny123)\
**Post date:** [December 2, 2018, 7:53pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/4 "2018-12-02T19:53:23Z")

</div>

You are absolutely correct. However, this solution seems a bit (subjectively) ugly. It seems to tell a potential reader of the code to look for the definition of `Query` inside of `Utils`, when it is actually defined in `Structs`. Is this a symptom of my project structure or something else?

---

<div class="post-metadata">

**Author:** ![Seanny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seanny123/32/5271_2.png) [@Seanny123](https://discourse.julialang.org/u/Seanny123)\
**Post date:** [December 2, 2018, 7:55pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/5 "2018-12-02T19:55:52Z")

</div>

This is a very good example of a small, but understandable package. Thank you for sharing it with me!

---

<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:** [December 2, 2018, 7:58pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/6 "2018-12-02T19:58:32Z")

</div>

Use `using ..Structs: Query` inside `Utils.jl` instead of reincluding the file. The `..` goes up one module higher and uses the existing `Structs` module defined there.

In general, you not have to include a file twice.

---

<div class="post-metadata">

**Author:** ![Seanny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seanny123/32/5271_2.png) [@Seanny123](https://discourse.julialang.org/u/Seanny123)\
**Post date:** [December 2, 2018, 8:08pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/7 "2018-12-02T20:08:38Z")

</div>

Do you mean switch to `using ..Structs: Query` inside `import_test.jl`. Either way, I still get `Structs not defined` error.

Also, you are free to stop debugging with me anytime you like.

---

<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:** [December 2, 2018, 8:31pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/8 "2018-12-02T20:31:07Z")

</div>

I meant to change `utils.jl` to look like:

```julia
module Utils

using ..Structs: Query

export dist

function dist(x::Query, y::Query)
    return (x.data - y.data) ^ 2
end

end

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 2, 2018, 9:29pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/9 "2018-12-02T21:29:39Z")

</div>

This is the same what I have written on SO (I have read the question there first).

However, you could define `Query` module inside a local Query package and then after adding this package you can do `using Query` inside `Utils` module and also do `using Query` in the outer scope and Julia will know that this is the same `Query` - right?

---

<div class="post-metadata">

**Author:** ![Seanny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seanny123/32/5271_2.png) [@Seanny123](https://discourse.julialang.org/u/Seanny123)\
**Post date:** [December 2, 2018, 9:41pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/10 "2018-12-02T21:41:13Z")

</div>

What do you mean by a “local Query package”?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 2, 2018, 9:44pm UTC](https://discourse.julialang.org/t/simple-example-package-to-imitate/18224/11 "2018-12-02T21:44:58Z")

</div>

> local Query package

I mean that this package does not have to be globally registered.

1. You can generate a package with `Query` as described in [https://docs.julialang.org/en/latest/stdlib/Pkg/#Creating-your-own-packages-1](https://docs.julialang.org/en/latest/stdlib/Pkg/#Creating-your-own-packages-1).
2. Then create a project for the main of your code as described in [https://docs.julialang.org/en/latest/stdlib/Pkg/#Creating-your-own-projects-1](https://docs.julialang.org/en/latest/stdlib/Pkg/#Creating-your-own-projects-1).
3. Then you activate this project and add the Query package form a **local** path as described in [https://docs.julialang.org/en/latest/stdlib/Pkg/#Adding-a-local-package-1](https://docs.julialang.org/en/latest/stdlib/Pkg/#Adding-a-local-package-1).

**EDIT**  
Then in your project, when it is activated you can load Query using `using Query` without having to write a dot or two dots in front of it.
