# Newbie question on search path

**URL:** https://discourse.julialang.org/t/newbie-question-on-search-path/12384
**Category:** New to Julia
**Created:** [July 15, 2018, 4:12am UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384 "2018-07-15T04:12:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![startz](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@startz](https://discourse.julialang.org/u/startz)
#### Post date: [July 15, 2018, 4:12am UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/1 "2018-07-15T04:12:24Z")

</div>

I should preface the question by saying I’m a Julia pre-newbie…I’ve read a bunch but haven’t written my first real program yet. (But I have 50 years of programming experience and write mostly in Matlab nowadays.) So please be gentle.

When I write in Matlab I break up my problem into small functions, one to a source file. For a given project all the functions go into one directory. I point Matlab at that directory and it knows to search for a function there first.

So what’s the equivalent in Julia. Is there a way to tell Julia where to look for files containing functions? Do I have to preface everything with a long list of #includes? Or is there just a different way to think about things in Julia? (I’m not writing large projects nor do they generally get used anywhere except on my desk.)

Thanks in advance.

---

<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: [July 15, 2018, 4:21am UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/2 "2018-07-15T04:21:45Z")

</div>

Welcome!

Julia code typically uses more, smaller functions, with many functions living within a single file. You don’t _have_ to do that, but I find that short functions are easier to describe, understand, and test. The fact that MATLAB only allows a single exported function per file, is, in my opinion, a significant downside to the language.

Since Julia code tends to have many functions, we need some way to organize them. The way we do that is with Modules. You can read about Modules in the manual here: [https://docs.julialang.org/en/stable/manual/modules/](https://docs.julialang.org/en/stable/manual/modules/)

For example, you can make a module by creating a file called `Foo.jl` and adding the following:

```julia
module Foo

function f(x)
  println(x)
end

function g(x, y)
  x + y
end

end

```

To use the module you’ve just defined, you need to tell Julia where to look for it. See: [https://docs.julialang.org/en/stable/manual/modules/#Module-file-paths-1](https://docs.julialang.org/en/stable/manual/modules/#Module-file-paths-1) All you need to do is add the folder containing `Foo.jl` to your `LOAD_PATH` variable with `push!(LOAD_PATH, "/path/to/folder")`. Then you can load your module with:

```julia
using Foo

Foo.f(1.0)

```

Note that the name of the module matches the name of the file: `module Foo` is in `Foo.jl`. The filename matters, and when you do `using Foo` Julia will expect to find `Foo.jl` or `Foo/src/Foo.jl` somewhere in `LOAD_PATH`.

If you want to call the `f` function without the `Foo.` prefix, you can modify your module:

```julia
module Foo

export f

function f(x)
  println(x)
end

function g(x, y)
  x + y
end

end

```

Then you can do:

```julia
using Foo
f(1.0)

```

If you don’t want to do the `push!(LOAD_PATH, ...)` every time you start Julia, you can either set the `JULIA_LOAD_PATH` environment variable, or you can just add that `push!` statement to your `~/.juliarc.jl` file. For more info see: [https://docs.julialang.org/en/stable/manual/getting-started/#Getting-Started-1](https://docs.julialang.org/en/stable/manual/getting-started/#Getting-Started-1)

If your module starts to get too big to comfortably remain in a single file, you can split it up into files however you like. See: [https://docs.julialang.org/en/stable/manual/modules/#Modules-and-files-1](https://docs.julialang.org/en/stable/manual/modules/#Modules-and-files-1)

Finally, as your code matures to the point where you’d like to share it with other people, you can turn it into a package, which other people will be able to install with `Pkg.add()` or `Pkg.clone()`. For more info see [https://docs.julialang.org/en/stable/manual/packages/](https://docs.julialang.org/en/stable/manual/packages/)

---

<div class="post-metadata">

### Author: ![startz](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@startz](https://discourse.julialang.org/u/startz)
#### Post date: [July 15, 2018, 1:50pm UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/3 "2018-07-15T13:50:14Z")

</div>

rdeits,

That’s a very clear answer and is much appreciated.

Let me ask if there is a Julia-ism that would effectively say

```julia
using _all-modules-in-directory-such-and-such_

```

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [July 15, 2018, 2:29pm UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/4 "2018-07-15T14:29:13Z")

</div>

I was just looking at this. I think you want to use `pushfirst!` so that your project or local path is searched first.

EDIT: or `unshift!` on Julia v0.6

---

<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: [July 15, 2018, 2:43pm UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/5 "2018-07-15T14:43:58Z")

</div>

No, there’s no command to do that. You could write a function that would list the directory’s contents and then load each module that it finds, but I’m not aware of such a function existing already.

Instead, if you have a collection of modules that are used together, you can just create them as nested modules inside another module. For example:

```julia
module MyProject

module Solvers

 f(x) = ...

end

module Visualization

g(x) = ...

end

end

```

Then you can do `using MyProject` and all of the modules inside it will be loaded. You can also put those inner modules in separate files and/or folders and `include()` them within your `MyProject.jl`.

A single module with several other modules inside it is very common in Julia packages, since it makes it easy to gather a bunch of related code while still keeping some logical separation between the various pieces.

---

<div class="post-metadata">

### Author: ![startz](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@startz](https://discourse.julialang.org/u/startz)
#### Post date: [July 15, 2018, 3:07pm UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/6 "2018-07-15T15:07:25Z")

</div>

Hadn’t thought of that. Thank you.

---

<div class="post-metadata">

### Author: ![startz](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@startz](https://discourse.julialang.org/u/startz)
#### Post date: [July 15, 2018, 3:08pm UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/7 "2018-07-15T15:08:21Z")

</div>

I see. This is helpful for me learning to think in Julia rather than just translating from Matlab. Thanks.

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [July 15, 2018, 5:30pm UTC](https://discourse.julialang.org/t/newbie-question-on-search-path/12384/8 "2018-07-15T17:30:42Z")

</div>

> [@rdeits](#):
>
> If you don’t want to do the `push!(LOAD_PATH, ...)` every time you start Julia, you can either set the `JULIA_LOAD_PATH` environment variable, or you can just add that `push!` statement to your `~/.juliarc.jl` file. For more info see: [https://docs.julialang.org/en/stable/manual/getting-started/#Getting-Started-1](https://docs.julialang.org/en/stable/manual/getting-started/#Getting-Started-1)

Be aware that in 0.7:  
The ~/.juliarc.jl file has been moved to ~/.julia/config/startup.jl and /etc/julia/juliarc.jl file has been renamed to /etc/julia/startup.jl [(#26161)](https://github.com/JuliaLang/julia/pull/26161).
