IT TIP

편집 후 활성 Julia 세션에서 모듈을 어떻게 다시로드합니까?

itqueen 2020. 12. 30. 19:57
반응형

편집 후 활성 Julia 세션에서 모듈을 어떻게 다시로드합니까?


2018 업데이트 : 이 질문에 대한 답변이 수년에 걸쳐 여러 번 변경되었으므로 모든 답변을 확인하십시오. 이 업데이트 시점에서 Revise.jl대답은 아마도 최상의 솔루션 일 것입니다.

"/SomeAbsolutePath/ctbTestModule.jl"파일이 있는데 그 내용은 다음과 같습니다.

module ctbTestModule
export f1
f1(x) = x + 1
end

"~ / .juliarc.jl"을 실행하는 터미널에서 Julia를 실행합니다. 시작 코드에는 다음 줄이 포함됩니다.

push!(LOAD_PATH, "/SomeAbsolutePath/")

따라서 즉시 Julia 콘솔에 입력 할 수 있습니다.

using ctbTestModule

내 모듈을로드합니다. 예상대로 f1(1)반환합니다 2. 이제 갑자기 편집하기로 결정했습니다 f1. 편집기에서 "/SomeAbsolutePath/ctbTestModule.jl"을 열고 내용을 다음과 같이 변경합니다.

module ctbTestModule
export f1
f1(x) = x + 2
end

이제 활성 Julia 세션에서 모듈을 다시로드하려고합니다. 나는 시도

using ctbTestModule

하지만 f1(1)여전히 반환합니다 2. 다음으로 시도합니다.

reload("ctbTestModule")

여기제안 된대로 , 그러나 f1(1)여전히 반환합니다 2. 마지막으로 다음을 시도합니다.

include("/SomeAbsolutePath/ctbTestModule.jl")

로 제안 여기에 있는, 아니 내가 현재 디렉토리가 "/ SomeAbsolutePath"하지 않을 수 있기 때문에 전체 절대 경로를 입력 할 필요가 있기 때문에 이상적. Warning: replacing module ctbTestModule유망하게 들리는 경고 메시지 표시 되지만 f1(1)여전히 2.

현재 Julia 세션을 닫고 새 세션을 시작하고를 입력 using ctbTestModule하면 이제 원하는 동작, 즉 f1(1)returns를 얻습니다 3. 그러나 분명히 Julia 다시 시작 하지 않고 이것을하고 싶습니다 .

그래서 내가 뭘 잘못하고 있니?

기타 세부 정보 : Ubuntu 14.04의 Julia v0.2.


이 문제의 기초 모듈을 다시로드하지만, 모듈의 일이 다시 정의 할 수 없다는의 합류이다 홈페이지를 ( 여기에 설명서를 참조 입니다 -) () 새로운 기능 작업 공간까지 적어도 이용할 수있게했다 7 월 13 일을 2014. 0.3 프리 릴리즈의 최신 버전이 있어야합니다.

workspace () 이전

다음과 같은 단순한 모듈을 고려하십시오.

module TstMod
export f

function f()
   return 1
end

end

그럼 사용 ....

julia> using TstMod

julia> f()
1

함수 f ()가 2반환 하도록 변경되고 모듈이 다시로드되면 f 는 실제로 업데이트됩니다. 그러나 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 () 사용

그러나 새 함수 작업 공간 () TstMod 를 다시로드 할 준비를하는 Main을 지 웁니다.

julia> workspace()

julia> reload("TstMod")

julia> using TstMod

julia> f()
2

또한 이전 MainLastMain 으로 저장됩니다 .

julia> whos()
Base                          Module
Core                          Module
LastMain                      Module
Main                          Module
TstMod                        Module
ans                           Nothing

julia> LastMain.f()
1

패키지 사용 Revise예 :

Pkg.add("Revise") # do this only once

include("src/my_module.jl")
using Revise
import my_module

새 REPL 세션에서 시작해야 할 수도 있습니다. 사용 주목 import대신이 using때문에 using펑션 재정의 않는다 Main(@Maciek하기 Leks 및 @waTeim하여 설명) 모듈.

다른 솔루션 :에 Revise.jl비해 두 가지 장점은 workspace()(1) 훨씬 빠르며 (2) 이 GitHub 문제workspace() 에서 논의한 것처럼 0.7에서 더 이상 사용되지 않는 미래 보장형이라는 것입니다 .

julia> VERSION
v"0.7.0-DEV.3089"

julia> workspace()
ERROR: UndefVarError: workspace not defined

GitHub 기여자가 추천했습니다 Revise.jl.

Should we add some mesage like "workspace is deprecated, check out Revise.jl instead"?

Even in Julia 0.6.3, the three previous solutions of workspace(), import, and reload fail when a module called other modules, such as DataFrames. With all three methods, I got the same error when I called that module the second time in the same REPL:

ERROR: LoadError: MethodError: all(::DataFrames.##58#59, ::Array{Any,1}) is ambiguous. Candidates: ...

I also got many warning messages such as:

WARNING: Method definition macroexpand(Module, ANY) in module Compat at /Users/mmorin/.julia/v0.6/Compat/src/Compat.jl:87 overwritten in module Compat at /Users/mmorin/.julia/v0.6/Compat/src/Compat.jl:87.

Restarting the Julia session worked, but it was cumbersome. I found this issue in the Reexport package, with a similar error message:

MethodError: all(::Reexport.##2#6, ::Array{Any,1}) is ambiguous.

and followed the suggestion of one contributor:

Does this happen without using workspace()? That function is notorious for interacting poorly with packages, which is partly why it was deprecated in 0.7.


In my humble opinion, the better way is to use import from the very beginning instead of using for the reported issue.

Consider the module:

module ModuleX1
  export produce_text
  produce_text() = begin
    println("v1.0") 
  end
  println("v1.0 loaded")
end

Then in REPL:

julia> import ModuleX1
v1.0 loaded

julia> ModuleX1.produce_text()
v1.0

Update the code of the module and save it:

module ModuleX1
  export produce_text
  produce_text() = begin
    println("v2.0")  
  end
  println("v2.0 loaded")
end

Next, in the REPL:

julia> reload("ModuleX1")
Warning: replacing module ModuleX1
v2.0 loaded

julia> ModuleX1.produce_text()
v2.0

Advantages of using import over using:

  • avoiding ambiguity in function calls (What to call: ModuleX1.produce_text() or produce_text() after reloading?)
  • do not have to call workspace() in order to get rid of ambiguity

Disadvantages of using import over using:

  • a fully qualified name in every call for every exported name is needed

Edited: Discarded "full access to the module, even to the not-exported names" from "Disadvantages..." according to the conversation below.


In julia v0.6.0 it seems like using workspace() is no longer necessary: I can simply reload(MyModule) in an active REPL session, and it works as expected (chages made to the source file that contains MyModule are reflected in the active REPL session).

This applies to modules that have been brought into scope by either import or using


I wanted to create a new module from scratch, and tried the different answers with 1.0 and didn’t get a satisfactory result, but I found the following worked for me:

From the Julia REPL in the directory I want to use for my project I run

pkg> generate MyModule

This creates a subdirectory like the following structure:

MyModule
├── Project.toml
└── src
    └── MyModule.jl

I put my module code in MyModule.jl. I change to the directory MyModule (or open it in my IDE) and add a file Scratch.jl with the following code:

Pkg.activate(“.”)
using Revise
import MyModule

Then I can add my code to test below and everything updates without reloading the REPL.

ReferenceURL : https://stackoverflow.com/questions/25028873/how-do-i-reload-a-module-in-an-active-julia-session-after-an-edit

반응형