Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia相对路径和导入自定义模块_Julia - Fatal编程技术网

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中运行命令,如下所示:
  • 所以我的问题是。。。 Q1)如何解决上述问题? 问题2)有没有办法避免这种混乱的相对路径?(是否可以修复与代码位置无关的路径?)

    以下是与该问题相关的简要代码,希望对您有所帮助:

  • main.jl
  • 型号/航空航天系统.jl
  • algorithm/controller.jl

  • 嘿,Jinrae,我在一些项目中也有类似的设置。我所做的是创建一个包本身(请参见PackageTemplate.jl),然后导入/包含所有模块和文件。然后,在其他地方,如果您想使用您编写的任何代码,您只需使用“包的名称”,它就会引入所有代码@谢谢,我来看看包裹:)@logankilprick抱歉,我还是不明白。。。e、 例如,我通过
    PkgTemplates.jl
    生成了我自己的pkg
    MyPkg
    。然后,有一个文件
    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