Julia相对路径和导入自定义模块
我是朱莉娅的新手。 我真的对使用自定义模块感到困惑。 注意:我通过Julia相对路径和导入自定义模块,julia,Julia,我是朱莉娅的新手。 我真的对使用自定义模块感到困惑。 注意:我通过vim工作,而不是Juno 例如,这是我的目录树(请忽略tmp.txt) 我通常在main.jl中编写主要函数,并从其他目录导入(使用)模块,例如算法和模型 就我而言,有几个恼人的问题: 如果我想在model/aerospace\u system.jl中使用algorithm/Controller.jl中的模块Controller(假设存在),那么我似乎必须在model/aerospace\u system.jl中添加includ
vim
工作,而不是Juno
例如,这是我的目录树(请忽略tmp.txt
)
我通常在main.jl
中编写主要函数,并从其他目录导入(使用)模块,例如算法
和模型
就我而言,有几个恼人的问题:
model/aerospace\u system.jl
中使用algorithm/Controller.jl中的模块Controller
(假设存在),那么我似乎必须在model/aerospace\u system.jl中添加include(“../algorithm/Controller.jl”)
,即使我在main.jl
中执行函数
控制器导入后
包括(“../algorithm/controller.jl”);使用模型/航空航天系统.jl中的.Controller
,
我不能直接使用包含的函数,例如,Controller
中的nocontrol
。
在本例中,我在Julia REPL中运行命令,如下所示:
main.jl
型号/航空航天系统.jl
algorithm/controller.jl
嘿,Jinrae,我在一些项目中也有类似的设置。我所做的是创建一个包本身(请参见PackageTemplate.jl),然后导入/包含所有模块和文件。然后,在其他地方,如果您想使用您编写的任何代码,您只需使用“包的名称”,它就会引入所有代码@谢谢,我来看看包裹:)@logankilprick抱歉,我还是不明白。。。e、 例如,我通过
PkgTemplates.jl
生成了我自己的pkgMyPkg
。然后,有一个文件MyPkg/src/MyPkg.jl
和MyPkg/test/runtests.jl
。在MyPkg/test/runtests.jl
中,使用MyPkg不会发生错误。但是,如果我创建一个文件MyPkg/src/OtherModule.jl
,并且使用OtherModule
会报告错误(Julia说没有这样的模块)。我搞错什么了?嗯。。。也许我知道了。生成几个包,并通过使用名称自然导入这些包来使用它们。谢谢
.
├── algorithm
│ └── controller.jl
├── debug.jl
├── main.jl
├── model
│ └── aerospace_systems.jl
└── tmp.txt
2 directories, 5 files
julia> includet("main.jl") # using Revise
julia> run_main()
ERROR: UndefVarError: nocontrol not defined
...
includet("model/aerospace_systems.jl")
using DynamicalSystems
using .AerospaceSystems
using Plots
ENV["GKSwstype"]="nul" # do not show figures automatically
function simulate_linear(x0; T=10., dt=0.01)
system = linearsystem(x0=x0)
traj = trajectory(system, T, dt=dt)
ts = 0:dt:T
return ts, traj
end
function draw_plot(ts, traj)
p = plot(ts, traj,
xlabel="t (s)", label=["x1" "x2" "x3"], linewidth=1.5)
save_path = "data/plot.pdf"
save_dir = dirname(save_path)
if !isdir(save_dir)
mkdir(save_dir)
end
savefig(p, save_path)
end
function run_main()
## main
x0 = 10*ones(3)
ts, traj = simulate_linear(x0)
## plot
draw_plot(ts, traj)
end
module AerospaceSystems
export linearsystem
using Debugger
using StaticArrays
using DynamicalSystems
const CDS = ContinuousDynamicalSystem
include("../algorithm/controller.jl")
import .Controller
function linearsystem(;x0=zeros(3), controller=nocontrol)
lineardynamics_with_controller(x, p, t) = lineardynamics(x, p, t; controller=controller)
return CDS(lineardynamics_with_controller, x0, nothing)
end
module Controller
export nocontrol, negativelinearfeedback
using LinearAlgebra
function nocontrol(x, t; dim=1)
return zeros(dim, 1)
end
function negativelinearfeedback(x, t; K=I)
return -K*x
end
end