# How are modules found?

**URL:** <https://discourse.julialang.org/t/how-are-modules-found/8723>\
**Category:** New to Julia\
**Tags:** package\
**Created:** [February 1, 2018, 2:19am UTC](https://discourse.julialang.org/t/how-are-modules-found/8723 "2018-02-01T02:19:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [February 1, 2018, 2:19am UTC](https://discourse.julialang.org/t/how-are-modules-found/8723/1 "2018-02-01T02:19:18Z")

</div>

Is there ever a reason to call `Base.require` directly, or is it a function that  
is used only internally by `import`/`using`, and I should never call it directly  
and forget it exists?

Since the argument to `require` is a module name, not a filename, which source file is loaded? How does it make the connection between the module name and the file name? The documentation says “When searching for files, require first looks for package code under Pkg.dir(), then tries paths in the global array LOAD\_PATH”, but that seems somewhat ambiguous. By extension, since `require` provides the implementation for `import`/`using`, my question is really about how modules are found in those instances, too.

The Julia documentation on Modules says “Files and file names are mostly unrelated to modules; modules are associated only with module expressions”. However, some experimentation seems to indicate that there is a pretty direct link: `using MyModule` has worked for me in exactly two cases:

1. There is a file `MyModule/src/MyModule.jl` in `Pkg.dir()` that defines a module `MyModule`

2. There is a file `MyModule.jl` in any directory in `LOADS_PATHS`. The behavior is pretty much the same as for the package (the file must define the module `MyModule` and it behaves in the same way regarding other/additional modules defined in the same file.

Are these empirical rules I’m observing the official behavior? Is this spelled  
out somewhere in the documentation, in a place I didn’t find?

So in essence it looks to me like Julia uses “Packages” as a way to distribute exactly one module (possibly with submodules, but submodules don’t seem to be encouraged, or at least I haven’t found any of the major packages that use them.

One more question: Is there a way to have multiple directories in `Pkg.dir()`?

---

<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 1, 2018, 4:43am UTC](https://discourse.julialang.org/t/how-are-modules-found/8723/2 "2018-02-01T04:43:19Z")

</div>

I asked something similar here:

> [@Packages, modules, and files](https://discourse.julialang.org/t/packages-modules-and-files/7652):
>
> The exact connection between packages, modules, and source files is unclear to me, even after reading the Pkg3 Julep and the relevant chapters of the documentation. Please note that this is not a criticism of either the current state or what will replace it with Pkg3, I am just trying to understand how things work. My understanding of the status quo is the following: A package is a collection of files and metadata. The have names like Foo.jl. A module is a piece of source code, eg module …

but did not get an answer.

My understanding is that things change with Pkg3 anyway.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [February 4, 2018, 7:36pm UTC](https://discourse.julialang.org/t/how-are-modules-found/8723/3 "2018-02-04T19:36:53Z")

</div>

Ok… at least you’ve found the same behavior! I looked at the plans for Pkg3, and it seems like this will only add to the places where Packages may be found. Nonetheless, I think any `using`/`import` will still require that any module `MyModule` is in a file `MyModule.jl`, and (if not directly in `LOAD_PATH`) in a package `MyModule`.

This requirement should probably just be added to the documentation. Maybe I’ll do a pull request…
