# Using an already-loaded module in 0.7

**URL:** <https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 11, 2018, 2:06pm UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587 "2018-06-11T14:06:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [June 11, 2018, 2:06pm UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/1 "2018-06-11T14:06:38Z")

</div>

When doing the following in 0.7:

```julia
module Foo end
using Foo

```

This yields:

```nohighlight
ERROR: ArgumentError: Module Foo not found in current path.
Run `Pkg.add("Foo")` to install the Foo package.

```

I found I could do `using Main.Foo` instead, but is there a way to avoid this? My usecase is CxxWrap.jl, where modules are created using a function call instead of by regularly loading a package.

---

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [June 11, 2018, 2:36pm UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/2 "2018-06-11T14:36:09Z")

</div>

That module is defined in `Main`, which is the module where one executes all the expressions of the REPL, and so on. Just call it by `Main.Foo` or simply `.Foo` if you are still in `Main`.

To check on which module are you use `@ __module__ `.

---

<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:** [June 11, 2018, 2:59pm UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/3 "2018-06-11T14:59:06Z")

</div>

See also: [Creating modules in Julia v0.7-alpha - #3 by Roger-luo](https://discourse.julialang.org/t/creating-modules-in-julia-v0-7-alpha/11475/3)

`using .Foo` is unambiguous and will just work in v0.6 and v0.7.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [June 11, 2018, 3:26pm UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/4 "2018-06-11T15:26:07Z")

</div>

Thanks, missed the other topic about this. So my next question is, is it a good idea to still load modules in `Main` then? There is now a ` __toplevel__ ` module in Base:

```julia
# This is used as the current module when loading top-level modules.
# It has the special behavior that modules evaluated in it get added
# to the loaded_modules table instead of getting bindings.
baremodule __toplevel__
using Base
end

```

But when I use that (i.e. `@eval Base. __toplevel__ module Foo end`), the module is invisible. Since the default behavior is no longer to load modules into `Main`, it seems that for CxxWrap I should also avoid doing that.

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [June 13, 2018, 2:35am UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/5 "2018-06-13T02:35:16Z")

</div>

> [@barche](#):
>
> But when I use that (i.e. `@eval Base. __toplevel__ module Foo end` ), the module is invisible.

The normal code-loading logic requires (!) modules to (seem to) be defined by files in `LOAD_PATH` etc. I suspect that is a mistake, but in any case one can get around this as follows:

```julia
# reverse lookup of pkg id 
# (We can't simply use the module as an index because it's not bound)
function findmod(f::Symbol)
    for (u,v) in Base.loaded_modules
        (Symbol(v) == f) && return u
    end
    nothing
end

# substitute for `import Foo`:
Foo = Base.loaded_modules[findmod(:Foo)]

# substitute for `using Foo`:
ccall(:jl_module_using, Cvoid, (Any, Any), MyModule,
      Base.loaded_modules[findmod(:Foo)])

```

---

<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:** [June 13, 2018, 9:02pm UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/6 "2018-06-13T21:02:29Z")

</div>

Messing around with `Base. __toplevel__ ` or `Base.loaded_modules` is not recommended and may break at any time in a 1.x release. I don’t really understand the use case for CxxWrap well enough to give advice on a better way to do this. @barche, can you elaborate?

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [June 15, 2018, 11:29am UTC](https://discourse.julialang.org/t/using-an-already-loaded-module-in-0-7/11587/7 "2018-06-15T11:29:15Z")

</div>

> [@StefanKarpinski](#):
>
> I don’t really understand the use case for CxxWrap well enough to give advice on a better way to do this. @barche, can you elaborate?

The problematic code is [this](https://github.com/JuliaInterop/CxxWrap.jl/blob/master/src/CxxWrap.jl#L135-L137):

```julia
function create_module(name::String, parent::Module)
  return Core.eval(parent, :(module $(Symbol(name)) end))
end

```

This gets called by `wrap_modules(cpp_lib)` to create a Julia module for every module declared on the C++ side and compiled into the `cpp_lib`. The parent module is `Main` by default, so in Julia 0.7 using the generated modules would require the leading “.”.

I think @Ralph_Smith provided a viable workaround, but on further reflection I think using the following approach is better:

```julia
module CppFoo

using CxxWrap

wrap_module(cpp_mod)

end

```

This approach is already supported as an alternative, but I intend to make it the only way to declare a module. That way no hacking into Julia internals is needed and everything works just like a normal Julia module. The only downside is that it’s a bit more verbose when there are multiple modules in the C++ lib, but in practice it’s often required to add some Julia code to the modules, so manually declaring the module is the only way to go anyway.
