# How to add a folder path in your .jl script

**URL:** <https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901>\
**Category:** New to Julia\
**Tags:** code-organization\
**Created:** [May 27, 2021, 5:26am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901 "2021-05-27T05:26:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sxd190026](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sxd190026/32/25250_2.png) [@sxd190026](https://discourse.julialang.org/u/sxd190026)\
**Post date:** [May 27, 2021, 5:26am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/1 "2021-05-27T05:26:46Z")

</div>

I recently moved from matlab to julia and I want to add a folder path in my script (similar to addpath(“folder path”) ) in my julia script, so that I can use any function files inside that folder? For example, I am using something like this:

push!(LOAD\_PATH,“folder path”)  
include(“file\_name.jl”)

resulting in error. I also tried require(“file\_name.jl”), still resulting in error. Can anyone help plz?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 27, 2021, 5:51am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/2 "2021-05-27T05:51:13Z")

</div>

What’s the error? I would expect `include("file_name.jl")` to work in this case.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 27, 2021, 7:11am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/3 "2021-05-27T07:11:26Z")

</div>

`LOAD_PATH` is relevant to loading of packages and modules via `import` or `using`. It’s content has no bearing on code inclusion with `include`.

You either need to specify the full path when using `include` (relative to the path of the calling file, or the working dir in the REPL), or wrap the functions in a module and load it via `import`.

The latter method is certainly the more julian.

See also: [Newbie question on search path](https://discourse.julialang.org/t/newbie-question-on-search-path/12384)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 27, 2021, 2:24pm UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/4 "2021-05-27T14:24:48Z")

</div>

You shouldn’t be using `LOAD_PATH` for this. You should have a `Project.toml` or `Manifest.toml` in your directory and use `import Pkg; Pkg.activate(".")`

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [May 27, 2021, 2:34pm UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/5 "2021-05-27T14:34:19Z")

</div>

It could be a problem with “folder path”. Does it exist? Do you have the separators right? Are you using the tilde `~`? if so, try something like this:

```
dir = expanduser("~/path/to/file/")
push!(LOAD_PATH, dir)

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 27, 2021, 6:31pm UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/6 "2021-05-27T18:31:28Z")

</div>

> [@sxd190026](#):
>
> I recently moved from matlab to julia and I want to add a folder path in my script (similar to addpath(“folder path”) ) in my julia script, so that I can use any function files inside that folder?

Short answer: Julia doesn’t work like Matlab in that way.

For a longer answer, first some background for those who don’t know Matlab. In Matlab functions are organized with one public function per file, and the filename must match the function name. There may be additional private subfunctions in each file, but those are only available within the file (unless you do some trickery). To decide which files, and thus functions, are available for use, Matlab has a `path` variable where you add directories, and all `*.m` files within those directories are automatically imported.

The closest Julia analogy is to consider each such directory as a package, and each file corresponds to an exported function. Then adding a Julia package directory to `LOAD_PATH` becomes similar to Matlab’s `addpath`, with the difference that the actual activation of a package is triggered by a `using` statement.

There are more and arguably better options in Julia but the most direct translation of a Matlab code base would be to convert each directory with functions to a package, such that this directory structure

```julia
stuff
├── a.m
└── b.m

```

becomes

```julia
Stuff
├── Project.toml
└── src
    ├── a.jl
    ├── b.jl
    └── Stuff.jl

```

where `Stuff.jl` contains

```julia
module Stuff
export a, b
include("a.jl")
include("b.jl")
end

```

Notice, however, that if `a.m` and `b.m` have subfunctions, those may clash and need to be renamed so they don’t.

Disclaimer: I haven’t used Matlab for several years and never a more recent version than ~2013, so if things have changed drastically in the last few years, this might be outdated advice.

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [May 28, 2021, 1:30am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/7 "2021-05-28T01:30:04Z")

</div>

Having also come from Matlab some time ago, I can definitely second the advice of organizing your code by making your own packages.

Not so much related to the above scenario, but one other trick that can be useful for quick one-off scripts is `@ __DIR__ ` – e.g.:

```julia
cd(@ __DIR__ )
# then do something with files the script directory

```

or

```julia
read/dlmread/include/whatever(@ __DIR__ *"somefile.ext")

```

etc.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 28, 2021, 1:51am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/8 "2021-05-28T01:51:57Z")

</div>

### Almost there

I myself `push!()` my custom package library onto `LOAD_PATH` in much the same way as you do in your example @sxd190026.

And, despite some of the comments that almost seem to imply otherwise, I don’t believe this is necessarily a bad technique.

In fact, I find it is one of the easiest ways to add your custom library of utilities to new Julia projects. I think the only real drawback is that it doesn’t lend itself quite as well to sharing these Julia environments with other co-workers.

### `LOAD_PATH`

As @skleinbo said:

- `push!(LOAD_PATH, "/abs/path/to/my/package_lib")` essentially adds a _ **package library** _ folder to Julia’s search path.
- That means `include()` (_file_ loading mechanism) is unaffected by `LOAD_PATH`.
- However: `using` & `import` (_package_ loading mechanisms) _ **are** _ affected by `LOAD_PATH`.

### `include()` vs (`using` or `import`)

`include("relative/path/to/file.jl")`:

- Loads code from `file.jl`.

`import MyAwesomePackage`:

- Loads entire “package” `MyAwesomePackage`.

`using MyAwesomePackage`:

- Essentially does the same as `import MyAwesomePackage`
- But also makes any symbols `export`-ed from `MyAwesomePackage` directly available in the caller’s scope.

Also note that both `using` and `import` can be used with already-loaded modules (instead of packages) - but we’ll ignore that complexity for now (I don’t find it to be as useful in typical cases - or this discussion).

### So what was missing?

There were really only 2 things missing from your solution:

1. You should import your code _ **as a package** _ instead of loading it _ **as a file** _.
  - `using MyAwesomePackage` – instead of – `include("file_name.jl)`

2. Packages need to be wrapped in a `module` - `end` block.

So, in `"/abs/path/to/my/package_lib"`, I suggest you create a test package:

```julia
#MySingleFilePackage.jl
module MySingleFilePackage #Module needs to match file/package name

dosomething() = println("Execute all algorithms!")

export dosomething #If you don't want people to prepend call with "MySingleFilePackage."

end #module MySingleFilePackage

```

And try it out in your script:

```julia
#Just try not to run push!() more than once in your scripts:
push!(LOAD_PATH, "/abs/path/to/my/package_lib")

using MySingleFilePackage
dosomething()

```

### What if I want to create a multi-file package?

Then I suggest you follow the structure for full-blown package described by @GunnarFarneback.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 28, 2021, 2:07am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/9 "2021-05-28T02:07:20Z")

</div>

> [@brenhinkeller](#):
>
> ```julia
> cd(@ __DIR__ )
> # then do something with files the script directory
> 
> ```

I would advise against this myself. In most cases, I find it is bad practice to change the user’s directory without her knowledge. This is typically done in things like bash files - but that’s because it typically only affects the run environment of that _ **particular** _ bash script.

> [@brenhinkeller](#):
>
> `read/dlmread/include/whatever(@ __DIR__ *"somefile.ext")`

Agreed. I find the `@ __DIR__ ` macro particularly useful for locating files you keep in the same directory as your code.

However, I would suggest you use the `joinpath()` function instead of concatenating strings with `*`. It can avoid occasional issues with ill-formed paths, and is also platform-aware (Uses `"\\"` path-separators on Windows machines):

```julia
data = dlmread(joinpath(@ __DIR__ , "somefile.ext"))

```

Also note that `@ __DIR__ ` is typically unnecessary when calling `include()` because `include()` already searches relative to the caller’s directory.

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [May 28, 2021, 2:09am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/10 "2021-05-28T02:09:22Z")

</div>

Yes, hence the note

> for quick one-off scripts

---

<div class="post-metadata">

**Author:** ![sxd190026](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sxd190026/32/25250_2.png) [@sxd190026](https://discourse.julialang.org/u/sxd190026)\
**Post date:** [May 28, 2021, 5:28am UTC](https://discourse.julialang.org/t/how-to-add-a-folder-path-in-your-jl-script/61901/11 "2021-05-28T05:28:47Z")

</div>

Thanks. wrapping the functions as modules makes it easy.
