# How to add a new module?

**URL:** <https://discourse.julialang.org/t/how-to-add-a-new-module/14734>\
**Category:** New to Julia\
**Created:** [September 9, 2018, 2:53pm UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734 "2018-09-09T14:53:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [September 9, 2018, 2:53pm UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734/1 "2018-09-09T14:53:32Z")

</div>

I created a directory ~/src/juliapractice (all the code I write is in ~/src) and used Pkg to create a project. I then added a file and tried to load it. The files look like this:  
Project.toml:

[code]authors = [“phma ”]  
name = “juliapractice”  
uuid = “a24d6352-b19b-11e8-179c-1589c5648784”  
version = “0.1.0”

[deps][/code]  
src/juliapractice.jl:

[code]module juliapractice

greet() = print(“Hello World!”)

end # module[/code]  
mandelbrot.jl:

[code]module mandelbrot

function julia\_set(z, c)  
n = 0  
while abs2(z) \< 4  
z = z^2 + c  
n += 1  
end  
n  
end

end[/code]  
Running julia in the juliapractice directory, I did this:

[code] (v1.0) pkg\> activate .

julia\> import mandelbrot  
ERROR: ArgumentError: Package mandelbrot not found in current path:

- Run `Pkg.add("mandelbrot")` to install the mandelbrot package.

Stacktrace:  
[1] require(::Module, ::Symbol) at ./loading.jl:817

julia\> import juliapractice  
[Info: Precompiling juliapractice [a24d6352-b19b-11e8-179c-1589c5648784]

julia\> julia\_set(0,0.5)  
ERROR: UndefVarError: julia\_set not defined  
Stacktrace:  
[1] top-level scope at none:0

julia\> greet()  
ERROR: UndefVarError: greet not defined  
Stacktrace:  
[1] top-level scope at none:0

julia\> juliapractice.julia\_set(0,0.5)  
ERROR: UndefVarError: julia\_set not defined  
Stacktrace:  
[1] getproperty(::Module, ::Symbol) at ./sysimg.jl:13  
[2] top-level scope at none:0

julia\> juliapractice.greet()  
Hello World!  
julia\>[/code]  
How do I tell Julia about the new file?

---

<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:** [September 9, 2018, 3:19pm UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734/2 "2018-09-09T15:19:51Z")

</div>

please quote your code

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 9, 2018, 3:56pm UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734/3 "2018-09-09T15:56:10Z")

</div>

Julia is expecting your `juliapractice` package to just define the `juliapractice` module, so having a second `mandelbrot` module at the top level of that file won’t work. You can define multiple modules at the same time, but they need to be nested inside the top-level module for the package. So this is ok:

```julia
module juliapractice

module SubModule1
  greet() = println("hello 1")
end

module SubModule2
  foo() = 5
end
end

```

The rest of the issues you’re seeing are just the normal way that `import` works in Julia: when you `import Foo` you need to do `Foo.bar()`. See: [Modules · The Julia Language](https://docs.julialang.org/en/v0.6.0/manual/modules/#Summary-of-module-usage-1)

---

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [September 14, 2018, 11:32am UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734/4 "2018-09-14T11:32:03Z")

</div>

Trying again to post the files:  
Project.toml:

```julia
authors = ["phma <snip>"]
name = "juliapractice"
uuid = "a24d6352-b19b-11e8-179c-1589c5648784"
version = "0.1.0"

[deps]

```

juliapractice.jl:

```julia
module juliapractice

greet() = print("Hello World!")

end # module

```

mandelbrot.jl:

```julia
module mandelbrot

function julia_set(z, c)
    n = 0
    while abs2(z) < 4
        z = z^2 + c
        n += 1
    end
    n
end

end

```

If mandelbrot.jl should go in the juliapractice module, how do I tell Julia to compile both files together?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 14, 2018, 11:42am UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734/5 "2018-09-14T11:42:03Z")

</div>

You put `include("mandelbrot.jl")` somewhere inside the `juliapractice` module.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 18, 2018, 5:57pm UTC](https://discourse.julialang.org/t/how-to-add-a-new-module/14734/6 "2018-09-18T17:57:49Z")

</div>

Also, try to use `Pkg.generate` instead of manually creating and tracing `Project.toml`  
More [here](https://docs.julialang.org/en/v1/stdlib/Pkg/#Creating-your-own-packages-1).
