# The Julia REPL: How clean environment variables. VSCODE

How to clean, delete the environment variables initialized during the running of a script in REPL; to avoid conflicts when running another script with variables with the same names but different types.
How would it be in VSCODE?

How to reset environment variables:
an example of what happens to me;
studying the Differential Equations tutorial,
when running the following command lines:

``````#https://diffeq.sciml.ai/stable/tutorials/dae_example/
function f(out,du,u,p,t)
out = - 0.04u              + 1e4*u*u - du
out = + 0.04u - 3e7*u^2 - 1e4*u*u - du
out = u + u + u - 1.0
end
#with initial conditions
u₀ = [1.0, 0, 0]
du₀ = [-0.04, 0.04, 0.0]
tspan = (0.0,100000.0)

using DifferentialEquations
differential_vars = [true,true,false]
prob = DAEProblem(f,du₀,u₀,tspan,differential_vars=differential_vars)

using Sundials
sol = solve(prob,IDA())

using Plots
plot(sol, xscale=:log10, tspan=(1e-6, 1e5), layout=(3,1))
``````

I get the result and the graphics as shown in the tutorial.

But running this other example:

``````#https://diffeq.sciml.ai/stable/tutorials/advanced_ode_example/#Handling-Mass-Matrices
using DifferentialEquations
function rober(du,u,p,t)
y₁,y₂,y₃ = u
k₁,k₂,k₃ = p
du = -k₁*y₁ + k₃*y₂*y₃
du =  k₁*y₁ - k₃*y₂*y₃ - k₂*y₂^2
du =  y₁ + y₂ + y₃ - 1
nothing
end
M = [1. 0  0
0  1. 0
0  0  0]
f= ODEFunction(rober,mass_matrix=M)
prob_mm = ODEProblem(f,[1.0,0.0,0.0],(0.0,1e5),(0.04,3e7,1e4))
sol = solve(prob_mm,Rodas5(),reltol=1e-8,abstol=1e-8)

plot(sol, xscale=:log10, tspan=(1e-6, 1e5), layout=(3,1))
``````

``````julia> f= ODEFunction(rober,mass_matrix=M)
ERROR: invalid redefinition of constant f
Stacktrace:
 top-level scope
@ REPL:1
``````

which solved it by changing the line of code:

f = ODEFunction (rober, mass_matrix = M)

to

ff = ODEFunction (rober, mass_matrix = M)

There is some command that, we can put at the beginning of the lines of code, and initialize the environment.

In R with the following command line:

rm(list = ls ())

In Matlab
clear all

What would it be like in Julia with VSCODE?

There used to be a command for this, but it was causing problems and was removed. I’m not an expert, but the two ways I know to do this are:

1. you wrap everything into a function like
``````function tmp1()
end
tmp1()
``````

and then you move up to `tmp2()`, etc…

1. This is what I actually do: Assign keyboard shortcuts to exiting and restarting Julia (or memorize the default shortcuts). Back in the early days that used to be hopeless, but in Julia 1.6 it’s ultra fast. Example (you can set your own key combos in a similar way, go to `Preferences -> Keyboard shortcuts` and set key combos for `"language-julia.startREPL"` and `"workbench.action.terminal.kill"`, also try `"language-julia.stopREPL"`) (if you don’t know what I’m talking about, read this):
``````[
{
"key": "f2",
"command": "-editor.action.rename",
"when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
},
{
"key": "f2",
"command": "language-julia.startREPL"
},
{
"key": "alt+j alt+o",
"command": "-language-julia.startREPL"
},
{
"key": "cmd+f2",
"command": "workbench.action.terminal.kill"
},
]
``````

That’s all I know. I’ll be watching this discussion to see if there are better ways. Rather than wrapping the script in a function, I would do it in a module. That way, you can keep the lines with `using` and other things dealing with globals, more or less as when you run the script from `Main`. Moreover, after running the script you can then inspect the globals, just prepending the name of the module (e.g. if the module was `Mod`, you can look into its variables `x`, `y`, etc. as `Mod.x`, `Mod.y`…). If you re-run the script, you’ll get a warning about redefinition of the module, but it works anyway.

Regarding killing and restarting the REPL in VS Code, there are already default keyboard shortcuts: `Alt + J, Alt + O` to start it, and `Alt + J, Alt + K` to kill it.

1 Like

A practical, minimal example of doing such things with modules. This is the script:

``````module Mod
x = 1
end
``````

Then in the REPL, after running that script:

``````julia> Mod.x
1
``````

If then I rewrite the script as:

``````module Mod
y = 2
end
``````

I’ll get a warning when re-running it:

``````WARNING: replacing module Mod.
``````

But nevertheless:

``````julia> Mod.y
2

julia> Mod.x # it does not exist anymore
ERROR: UndefVarError: x not defined
``````

You can even redefine types inside the module this way - although that may cause problems if you use such types outside the module.

2 Likes