Is there a way to undo `using` in Julia?
Asked Answered
M

3

17

Suppose in the Julia console I do

using Gadfly

And then realise I actually want to use a different plot command

using Winston

It seems that plot() will still use the Gadfly version. Is there any way to stop using Gadfly?

using !Gadfly
!using Gadfly
unuse Gadfly

Something like that?

Mauretta answered 27/3, 2016 at 15:49 Comment(1)
I
13

As mentioned in the link by @Jubobs, there is currently not a way to selectively stop using a package, or selectively remove a definition from the REPL (similar to Matlab's clear command if you are familiar with it). So the short answer is no.

However, you can reference functions from specific modules by using Gadfly.plot(), or Winston.plot(). This doesn't provide you a solution to your problem if you have already written the code, but it is still an option for future work.

There is the workspace() command but that will remove everything from the Main module and will import a fresh Julia environment. You will lose all the functions and variables you have defined... so use it wisely

As @Matt B pointed out, you don't actually lose your functions and variables. They are moved to a module called LastMain. So if I have a function defined called myfunc(), and I call workspace(), then attempting to call myfunc() at the REPL will result in an UndefVarError. However, you will still be able access this function by calling LastMain.myfunc(). This is true of anything that was defined in the REPL before your call to workspace().

Irremediable answered 27/3, 2016 at 17:14 Comment(3)
You don't really lose them, they just get stashed into the LastMain module.Almeta
workspace() was deprecated in Julia 0.7 and removed in 1.0 (see Julia 0.7 release notes, deprecations section and Pull request #25046 merged in late 2017). There is a still opened issue #32129 about adding a restart() command.Swage
See also Julia's forum thread How to clear variables and/or whole work space with messages from 2018 to early 2022.Swage
S
7

As others have pointed out, you cannot unload a package in Julia. The only way to do that would be to restart the Julia repl. However, if you want to use a specific function from a package, you can us Winston.plot() or Gadfly.plot(). Thus will ensure, you're using the right function from the desired namespace.

Serow answered 27/3, 2016 at 17:8 Comment(0)
P
0

I met the same problem when comparing different plotting frameworks. The final solution is to use import instead of using. You have to write a little more code than before:

Gadfly.plot(...)
Winston.plot(...)

vs

plot(...)

However, there's no need to restart REPL. For JIT language like Julia, restarting wastes too much compilation time.

Pendergrass answered 24/1, 2023 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.