编辑后如何在活动的 Julia 会话中重新加载模块? [英] How do I reload a module in an active Julia session after an edit?
问题描述
2018 年更新:请务必检查所有回复,因为多年来,此问题的答案已多次更改.在此更新时,Revise.jl
答案可能是最佳解决方案.
2018 Update: Be sure to check all the responses, as the answer to this question has changed multiple times over the years. At the time of this update, the Revise.jl
answer is probably the best solution.
我有一个文件/SomeAbsolutePath/ctbTestModule.jl",其中的内容是:
I have a file "/SomeAbsolutePath/ctbTestModule.jl", the contents of which are:
module ctbTestModule
export f1
f1(x) = x + 1
end
我在运行~/.juliarc.jl"的终端中启动 Julia.启动代码包括以下行:
I fire up Julia in a terminal, which runs "~/.juliarc.jl". The startup code includes the line:
push!(LOAD_PATH, "/SomeAbsolutePath/")
因此我可以立即在 Julia 控制台中输入:
Hence I can immediately type into the Julia console:
using ctbTestModule
加载我的模块.正如预期的那样 f1(1)
返回 2
.现在我突然决定要编辑f1
.我在编辑器中打开/SomeAbsolutePath/ctbTestModule.jl",并将内容更改为:
to load my module. As expected f1(1)
returns 2
. Now I suddenly decide I want to edit f1
. I open up "/SomeAbsolutePath/ctbTestModule.jl" in an editor, and change the contents to:
module ctbTestModule
export f1
f1(x) = x + 2
end
我现在尝试在我的活动 Julia 会话中重新加载模块.我试试
I now try to reload the module in my active Julia session. I try
using ctbTestModule
但是 f1(1)
仍然返回 2
.接下来我尝试:
but f1(1)
still returns 2
. Next I try:
reload("ctbTestModule")
按照此处的建议,但是f1(1)
仍然返回 2
.最后,我尝试:
as suggested here, but f1(1)
still returns 2
. Finally, I try:
include("/SomeAbsolutePath/ctbTestModule.jl")
按照此处的建议,不是 理想,因为我必须输入完整的绝对路径,因为当前目录可能不是/SomeAbsolutePath".我收到警告消息 Warning: replacement module ctbTestModule
听起来很有希望,但 f1(1)
仍然返回 2
.
as suggested here, which is not ideal since I have to type out the full absolute path since the current directory might not be "/SomeAbsolutePath". I get the warning message Warning: replacing module ctbTestModule
which sounds promising, but f1(1)
still returns 2
.
如果我关闭当前的 Julia 会话,开始一个新的会话,然后输入 using ctbTestModule
,我现在得到所需的行为,即 f1(1)
返回 <代码>3代码>.但显然我想在不重新启动 Julia 的情况下做到这一点.
If I close the current Julia session, start a new one, and type in using ctbTestModule
, I now get the desired behaviour, i.e. f1(1)
returns 3
. But obviously I want to do this without re-starting Julia.
那么,我做错了什么?
其他详细信息:Ubuntu 14.04 上的 Julia v0.2.
Other details: Julia v0.2 on Ubuntu 14.04.
推荐答案
这个问题的基础是重载一个模块的confluence,但是不能在模块中重定义一个东西Main(查看此处的文档)——即至少在 2014 年 7 月 13 日新功能 workspace() 可用之前.0.3 预发布的最新版本应该有它.
The basis of this problem is the confluence of reloading a module, but not being able to redefine a thing in the module Main (see the documentation here) -- that is at least until the new function workspace() was made available on July 13 2014. Recent versions of the 0.3 pre-release should have it.
考虑以下简单的模块
module TstMod
export f
function f()
return 1
end
end
然后使用它......
Then use it....
julia> using TstMod
julia> f()
1
如果函数 f() 被更改为 return 2 并且模块被重新加载,则 f 实际上被更新了.但没有在 Main 模块中重新定义.
If the function f() is changed to return 2 and the module is reloaded, f is in fact updated. But not redefined in module Main.
julia> reload("TstMod")
Warning: replacing module TstMod
julia> TstMod.f()
2
julia> f()
1
以下警告使问题清晰
julia> using TstMod
Warning: using TstMod.f in module Main conflicts with an existing identifier.
julia> using TstMod.f
Warning: ignoring conflicting import of TstMod.f into Main
使用工作空间()
但是,新函数 workspace() 清除 Main 准备重新加载 TstMod
Using workspace()
However, the new function workspace() clears Main preparing it for reloading TstMod
julia> workspace()
julia> reload("TstMod")
julia> using TstMod
julia> f()
2
此外,前一个Main存储为LastMain
Also, the previous Main is stored as LastMain
julia> whos()
Base Module
Core Module
LastMain Module
Main Module
TstMod Module
ans Nothing
julia> LastMain.f()
1
这篇关于编辑后如何在活动的 Julia 会话中重新加载模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!