编辑后,如何在活动的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: replacing 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.
那么,我在做什么错了?
So, what am I doing wrong?
其他详细信息:Ubuntu 14.04上的Julia v0.2.
Other details: Julia v0.2 on Ubuntu 14.04.
推荐答案
此问题的基础是重新加载模块的合流,但无法在模块 Main 中重新定义事物. em> (请参阅此处的文档)-即
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
然后使用它....
julia> using TstMod
julia> f()
1
如果将功能 f ()更改为 return 2 ,并且重新加载了模块,则实际上会更新 f .但未在模块主要中重新定义.
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屋!