# How to use local module within a Project directory?

**URL:** <https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358>\
**Category:** New to Julia\
**Tags:** module\
**Created:** [July 14, 2019, 6:48pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358 "2019-07-14T18:48:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [July 14, 2019, 6:48pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/1 "2019-07-14T18:48:30Z")

</div>

When I’m not in a Project directory, I can make a file like so:

```julia
# M1.jl
module M1
    export M1Type
    struct M1Type
        data::Real
    end
end

```

and then from that directory:

```julia
push!(LOAD_PATH, pwd()

```

and then

```julia
using M1

```

and that all works just fine. But if I’m in a Project directory (I have a Project.toml file), I can’t figure out how to use M1 in a similar way.

```julia
julia> using M1
ERROR: ArgumentError: Package M1 not found in current path:
- Run `import Pkg; Pkg.add("M1")` to install the M1 package.

```

I’ve tried to activate the local project and `]add M1`, `]add ./M1.jl`, `]add /full/path/to/M1.jl`, etc., but I’m reasonably sure that that’s just for packages, not local modules, and it just says:

```julia
ERROR: The following package names could not be resolved:
 * M1 (not found in project, manifest or registry)
Please specify by known `name=uuid`.

```

hence I don’t know how to make Julia “know” about my local M1 module when I’m in a Project directory.

(I know I can `include("M1.jl")`, but that does something a little different, such that anything that needs `M1` needs to know where it’s loaded.)

In [How to load a module from the current directory?](https://discourse.julialang.org/t/how-to-load-a-module-from-the-current-directory/21727), the “solution” was to delete the Project.toml file. Not what I’m looking to do! 🙂

I feel like I’ve read over the Module and Code Loading sections of the doc a bunch without figuring this out.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 14, 2019, 6:51pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/2 "2019-07-14T18:51:18Z")

</div>

`dev path/to/M1`? where `M1` has a Project file itself. If `M1` is not a package then do indeed use `include`.

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [July 14, 2019, 7:01pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/3 "2019-07-14T19:01:19Z")

</div>

Thanks for the quick reply! I’m unclear why that’s different when I’m in a Project directory than when I’m in a bare directory. It makes simple organizational tasks sort of strange, or I’m missing the right idiom for it perhaps.

As I understand it, in a bare directory, this works fine:

```julia
# M1.jl
module M1
    export M1Type
    struct M1Type
        data::Real
    end
end

# M2.jl
module M2
    using M1
    function f(x::M1Type)
        ...
    end
end

# At the REPL
using M1
using M2
f(M1Type(1))

```

On the other hand, the “Include” method forces M2 to have to know where M1 was loaded:

```julia
# M2.jl
module M2
    using ..M1 # How does M2 know where this was loaded, except that I've now hard-coded it?
    function f(x::M1Type)
        ...
    end
end

# At the REPL
include("M1.jl")
using M1
include("M2.jl")
using M2

```

so this is a fundamentally different way to organize my code, where modules now need to know where other modules are loaded, just due to the fact that I’m in a Project directory, if I understand correctly.

Is that correct?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 14, 2019, 8:09pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/4 "2019-07-14T20:09:02Z")

</div>

> [@tuckermcclure](#):
>
> Thanks for the quick reply! I’m unclear why that’s different when I’m in a Project directory than when I’m in a bare directory. It makes simple organizational tasks sort of strange, or I’m missing the right idiom for it perhaps.

I think there are some things in the loading of code that were put there to make the transition between the old package manager without Project files (0.6) to the new package manager (0.7) easier.

I would advise against touching `LOAD_PATH` and just use packages and `dev` for local code you want to reuse.

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [July 14, 2019, 8:37pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/5 "2019-07-14T20:37:06Z")

</div>

I think I follow you. I’m happy to leave the `LOAD_PATH` alone; I never actually liked that solution. You’re proposing to “upgrade” any local modules that need to be used in numerous places to full packages (with Project.toml files), and `]add` them to the active project by their local paths.

Alternately, I might stop breaking things down into several little modules and instead make one larger module and just include local files with implementation details, such as:

```julia
# M1.jl
struct M1Type # Note that there's no module this time.
    data::Real
end

# M2.jl
function f(x::M1Type)
    ....
end

# Elsewhere
module M3
    include("M1.jl")
    include("M2.jl")
    f(MyType(3)) # Use the code for something.
end

```

Previously, when I needed to break functionality down, I was defaulting to putting that functionality in a bunch of little modules, but I was finding that that didn’t work once upgrading my “directory with a bunch of .jl files in it” to a “Project” directory. I couldn’t tell what I was doing wrong. Simply breaking the functionality down into individual _files_ but not modules is probably the cleanest thing here, as far as I can tell.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 14, 2019, 8:44pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/6 "2019-07-14T20:44:03Z")

</div>

> [@tuckermcclure](#):
>
> You’re proposing to “upgrade” any local modules that need to be used in numerous places to full packages (with Project.toml files), and `]add` them to the active project by their local paths.

Yes, except probably use `]dev`

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [July 14, 2019, 8:53pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/7 "2019-07-14T20:53:41Z")

</div>

Thanks @kristoffer.carlsson!

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [April 1, 2020, 7:04pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/8 "2020-04-01T19:04:45Z")

</div>

Hi @kristoffer.carlsson, I know this is old, but as I’ve grown with this approach, there’s a new problem: ["Unsatisfiable requirements" when local packages use local packages (fixed in v1.4)](https://discourse.julialang.org/t/unsatisfiable-requirements-when-local-packages-use-local-packages/36807/)

---

<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:** [April 1, 2020, 7:14pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/9 "2020-04-01T19:14:52Z")

</div>

`dev` the packages in the right order or possibly together. Or if you have more than a few local packages to juggle with, consider taking the next step and make your own registry with [LocalRegistry](https://github.com/GunnarFarneback/LocalRegistry.jl). Alternatively, if they are of general interest, register them in the General registry.

---

<div class="post-metadata">

**Author:** ![peterj](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@peterj](https://discourse.julialang.org/u/peterj)\
**Post date:** [April 5, 2020, 9:45pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/10 "2020-04-05T21:45:55Z")

</div>

> As I understand it, in a bare directory, this works fine:
> 
> ```julia
> # M1.jl
> module M1
> export M1Type
> struct M1Type
> data::Real
> end
> end
> 
> # M2.jl
> module M2
> using M1
> function f(x::M1Type)
> ...
> end
> end
> 
> # At the REPL
> using M1
> using M2
> f(M1Type(1))
> 
> ```

This code doesn’t seem to compile anymore. I get the error `UndefVarError: M1 not defined`, even when I write `using .M1` instead.

Anyone know how to do this?

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [April 6, 2020, 2:42am UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/11 "2020-04-06T02:42:26Z")

</div>

Hi @peterj, I think your first `using M1` needs to be `using ..M1` (from the parent module). Your second `using M1` needs to be `using .M1` (for a local module), and the same for M2. Also, note that M2 does not export `f` presently, so you’ll need to add that.

---

<div class="post-metadata">

**Author:** ![peterj](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@peterj](https://discourse.julialang.org/u/peterj)\
**Post date:** [April 6, 2020, 2:54am UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/12 "2020-04-06T02:54:25Z")

</div>

Thanks, I didn’t know about the `..` thing

I still get an error, actually the error I was trying to reproduce when I found this thread!

```julia
# M1.jl
module M1
    export M1Type
    struct M1Type
        data::Real
    end
end

# M2.jl
module M2
    using ..M1
    export f
    function f(x::M1Type)
        
    end
end

# At the REPL
using .M1
using .M2
f(M1Type(1))

```

gives the error ` MethodError: no method matching f(::M1Type) Closest candidates are: f(::Main.M1.M1Type) Stacktrace:..`

Should I start a new thread about this?

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [April 6, 2020, 4:39pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/13 "2020-04-06T16:39:08Z")

</div>

Hi @peterj, I copied and pasted your code above into a .jl file and included it with no error. That was Julia v1.3. Maybe you should start a new thread and include the version of Julia?

---

<div class="post-metadata">

**Author:** ![peterj](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@peterj](https://discourse.julialang.org/u/peterj)\
**Post date:** [April 6, 2020, 4:45pm UTC](https://discourse.julialang.org/t/how-to-use-local-module-within-a-project-directory/26358/14 "2020-04-06T16:45:35Z")

</div>

Oh! Interesting. Yes this is julia v1.4. I shall do that.
