# How to add several subdirectories to LOAD\_PATH in startup.jl file?

**URL:** <https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195>\
**Category:** New to Julia\
**Tags:** question, code-organization\
**Created:** [February 5, 2020, 1:15am UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195 "2020-02-05T01:15:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [February 5, 2020, 1:15am UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/1 "2020-02-05T01:15:23Z")

</div>

If I have several subdirectories within a main directory, how would I add it to LOAD\_PATH without having to type each of them individually ?

Example:

```julia
 push!(LOAD_PATH,"/mainDir/") ## This is what I want to do

## I do not want to do this:
 push!(LOAD_PATH,"/mainDir/subdir1")
 push!(LOAD_PATH,"/mainDir/subdir2")
 ...
 push!(LOAD_PATH,"/mainDir/subdir300") 

```

---

<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:** [February 5, 2020, 6:55am UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/2 "2020-02-05T06:55:29Z")

</div>

You could use a loop.

But this is quite an unusual workflow, so if you explain what you are trying to do perhaps you will get suggestions for a better solution.

---

<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:** [February 5, 2020, 7:40am UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/3 "2020-02-05T07:40:41Z")

</div>

Something like this should work.

```julia
dir = "/mainDir/"
append!(LOAD_PATH, filter(isdir, joinpath.(dir, readdir(dir))))

```

But I agree with Tamas that there may be better workflow options.

---

<div class="post-metadata">

**Author:** ![sjbespa](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@sjbespa](https://discourse.julialang.org/u/sjbespa)\
**Post date:** [August 16, 2020, 10:43pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/4 "2020-08-16T22:43:25Z")

</div>

If there are better solutions, I’d like to hear about them too! I have a library with several dozens of modules that I regularly use for my research. Many are in their own directory because I may have julia, matlab and C++ versions of the algorithms, and python code for SymPy validation scripts, eg, and other test files. Maybe the way to think of the system/workflow is that the subdirectories are a bit more than “just” a module but way less than a package. So, it would be convenient to have a way to add the module subdirectories to the LOAD\_PATH? In the mean time, Gunnar’s suggestion is way better than creating the load path by hand.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 3, 2021, 1:55pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/5 "2021-12-03T13:55:36Z")

</div>

This is also my question, in Octave and Matlab it is possible to specify the root-directory for a private repository of modules.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 3, 2021, 3:04pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/6 "2021-12-03T15:04:57Z")

</div>

You can do that in Julia as well. If you put a path in the load path that contains a bunch of packages then you can load them.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 3, 2021, 3:10pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/7 "2021-12-03T15:10:31Z")

</div>

Maybe I missed a point. For me it does not work to have multiple folders and sub-folders that contain my modules. For example this could look like this:

```julia
..\MyLib\IO
..\MyLib\Plotting
..\MyLib\String
[...]
..\MyLib\Hardware

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 3, 2021, 5:16pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/8 "2021-12-03T17:16:40Z")

</div>

You do not have a main module that `include`s all other modules?

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 3, 2021, 5:33pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/9 "2021-12-03T17:33:14Z")

</div>

Thanks for the question! Under Octave and Matlab I have several modules for specific tasks / topics.  
Similar what you have in your Julia installation, there are also several folders  
containing the different modules.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 3, 2021, 7:46pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/10 "2021-12-03T19:46:41Z")

</div>

They need to be properly structured packages in order to be loaded as packages. That means they can either be a simple file names `Foo.jl` (defining a module named `Foo`) or a directory with the normal package structure.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 5, 2021, 7:49am UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/11 "2021-12-05T07:49:50Z")

</div>

I refer to the initial post of [josimar](https://discourse.julialang.org/u/josimar).  
I confirm that the observed behavior is still the same and it does not matter,  
if the last char is the directory separation char or not (“/” or “\”).

```julia
push!(LOAD_PATH, "/home/spofahl/julia/MyLib/")

```

is equal to

```julia
push!(LOAD_PATH, "/home/spofahl/julia/MyLib")

```

And the behavior is equal on MS-WINDOWS and Linux OS.  
My modules are reachable only, if they are located in the root  
directory of my private module-collection, if I put my module in  
a sub-directory e.g. “/home/spofahl/julia/MyLib/foo” it  
is not found ☹  
I need to add a push command for this sub-direcetory.  
Under octave I can achieve to add all sub-folders by means of the command:

```julia
 addpath(genpath("/home/spofahl/octave/MyLib"))

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 5, 2021, 2:35pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/12 "2021-12-05T14:35:39Z")

</div>

I don’t really understand what it is you’re trying to do, so it’s hard to explain why it is isn’t happening. Can you explain what you would like to have happen explicitly?

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 6, 2021, 8:45am UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/13 "2021-12-06T08:45:11Z")

</div>

Hi Stefan,  
I struggle to express myself in a way that makes it clear to experts like Tamas\_Papp and you.  
I have the impression I miss an important point about the Julian-way to do stuff I am used to  
do in a Matlab/Octave-way.  
I have to admit that I failed to do it in python (setting up my own collection of modules,  
I spent two years ago maybe 10 hours to figure out how to set-up my own private library of modules).  
In Julia I managed at least to publish one folder containing modules via the “push!()”-command.  
The objective is as follows:

- Build up my collection of modules
- Organize the related files in a folder structure
- Set-up a mechanism that takes care that new folders, containing modules and related files, are added to the search path for modules in an automated manner

Before I wrote these lines I spent (again) quite some times reading the Julia documentation  
but up to know the only thing I found was a reference to the push!()-command.

Regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 8, 2022, 10:28pm UTC](https://discourse.julialang.org/t/how-to-add-several-subdirectories-to-load-path-in-startup-jl-file/34195/14 "2022-02-08T22:28:40Z")

</div>

I have designed a function that does the job that me and “josimar” would like to see happening inside the `startup.jl`, I hope that it is of interest for others and maybe a starting point for improvement:

```julia
[...]
function addpath_genpath(s_dir_MyLib::AbstractString)
  if ~isdir(s_dir_MyLib)
    error(string("\"", s_dir_MyLib, "\" is not a dir!"))
  end
  for (root, dirs, files) in walkdir(s_dir_MyLib; follow_symlinks = false, onerror = identity)
    for i_file in files
      s_fn_body, s_fn_ext = splitext(i_file)
      if s_fn_ext == ".jl"
        s_file = joinpath(root, i_file)
        s_pattern = Regex(string("module[]+", s_fn_body))
        for i_line in eachline(open(s_file))
          if occursin(s_pattern, i_line)
            if ~any(occursin.(root, LOAD_PATH))
              push!(LOAD_PATH, root)
            end
          end
        end
      end
    end
  end
end

addpath_genpath("/home/yourname/julia/MyLib/")
[...]

```

There is one minor question:  
How can I avoid that function “addpath\_genpath” is listed inside the Workspace?
