Best way to import module variables to Main scope?

question

#1

I run into this problem all the time.

  • I want to develop a package where a bunch of pieces are hidden under the hood.
  • I like testing along the way in jupyter, so constantly have to use <Pkg>.blah(blah, blah)

Writing <Pkg> is cumbersome and emotionally taxing.


Is there a best practices way to safely implement the following code?

// i.e. for a stand-alone package

using DataStructures

cur_module = DataStructures
all_symbols = get_all_symbols(cur_module)

for cur_symbol in all_symbols
  cur_module.eval(parse("export $cur_symbol"))
end

(this could then tie into Revise.jl for some more magic…)


edit: an example get_all_symbols function can be seen here


#2

Perhaps an abbreviation?

import CumbersomeAndEmotionallyTaxingPackageName
const P = CumbersomeAndEmotionallyTaxingPackageName

@test P.blah(blah, blah) == blah

#3

That’s what I’ve been doing.

There still seems like there should be a way to do the described code.

This isn’t supposed to be performant or used in production.

// it’s just supposed to make Main into a sandbox for development


I was asking for suggestions to enhance the above code’s capabilities?

(and make it more idiomatic julia)


#4

The way you would do it in VSCode or Juno is to set the current module that code is evaluate din the same as the one of the package. Both these IDEs make that easy.

For the REPL you can use https://github.com/tshort/REPLinModule.jl.

Not sure how to best do it in IJulia. You can of course use

@eval <PKG> begin
    ....
end

but that might be more annoying than using the prefix everywhere.

You could add some hook into IJulia that changes the hardcoded Main here: https://github.com/JuliaLang/IJulia.jl/blob/6bbb0693363b9220f5ad6f0ecd8b46c909d95bdf/src/execute_request.jl#L159

Could be done something like https://github.com/JuliaLang/IJulia.jl/pull/651.


#5

With the PR accepted, I think this is resolved?


edit: also, I can’t get REPLinModule to plug-and-play as easy as described.

// have you had first experience getting it to work?


#6

The new IJulia command, set_current_module, works like a charm.


As for running in the REPL, I decided on the following hack:

// couldn’t get REPLinModule working :frowning:

# ~/.bashrc

alias fus="FUSION=true julia"

and

# ~/.juliarc

if haskey(ENV, "FUSION") && ENV["FUSION"] == "true"
  using FusionSystems

  for cur_symbol in names(FusionSystems, true)
    ( !Base.isidentifier(cur_symbol) ) && continue
    ( in(cur_symbol, (Symbol(FusionSystems), :eval)) ) && continue

    FusionSystems.eval(parse("export $cur_symbol"))
  end
end