Change "current working module" from REPL

In package development, I often find myself needing to bring lots of things into scope, so I can run bits and pieces as if my package were running them. Something like

julia> using Foo # or some package I'm developing
julia> using Foo: f, g, h, j
julia> using Bar # a dependency of `Foo`

etc.

It’s a lot of typing, and it’s very error prone. It would be a lot easier if we had a way to change modules from the REPL. Is this possible? Or maybe there’s an easy way to automate what I’m currently doing?

I don’t have an answer to your specific request, but if you only want to run a few isolated expressions, you can use @eval M expr to tell that you want to evaluate expr in module M. Or if it’s a larger piece of code that is written in a file, you can use M.include(file) or Base.include(M, file).

Also, since you are telling explicitly that you would like to change the working module in the REPL, I guess that you already know of how to do it in other environments. But anyway, I think that it is worth to comment that in VS Code you can choose the module where each file is meant to be run (also for “inline execution” with Alt+Enter etc.).

image

Really helpful suggestions, thank you @heliosdrm !

You could also give this Gist a go:

julia> changeREPLmodule(Base)

Base> @__MODULE__
Base

Base> Main.changeREPLmodule(Main)

Main> @__MODULE__
Main

Juno also automatically changes the REPL module to the current module, while VSCode doesn’t (as pointed out above).

This works beautifully! Unfortunately help mode and REPL completion doesn’t see that the module has changed.

There’s now a built in way to do this - type the module name in the REPL and press ALT-m. Or use the REPL.activate() function.

Implemented in REPL: allow switching contextual module by rfourquet · Pull Request #33872 · JuliaLang/julia · GitHub. Just leaving a note here so that people finding this old discussion via google can find the modern solution.