Julia : win下cmd和repl中执行.jl程序

在win平台下,可以在cmd(powershell)、julia repl中执行你的程序。

一、在cmd(powershell)下

1、path环境变量设定

首先,需要设置你的环境变量path,具体你找到julia.exe的路径,比如

C:\\Users\\songroom\\AppData\\Local\\Julia-1.3.1\\bin\\julia.exe

那么,添加到path中:

C:\\Users\\songroom\\AppData\\Local\\Julia-1.3.1\\bin\\

2、julia与cmd的切换

在这里插入图片描述
3、执行文件:julia +文件名

在这里插入图片描述
二、在repl下

julia> include("addPath.jl")
Now,is adding Julia working paths,and packages......
path arrangement and using package are finished!  costed 总共: 13.021000146865845 seconds
add path is ok

如果你介意()符号太多,你可以写一个宏,把()替换掉:

macro run(file)
    return :( include($(string(file))) )
end
julia> @run "addPath.jl"
Now,is adding Julia working paths,and packages......
path arrangement and using package are finished!  costed 总共: 0.1400001049041748 seconds
add path is ok

julia> @run addPath.jl
Now,is adding Julia working paths,and packages......
path arrangement and using package are finished!  costed 总共: 0.1400001049041748 seconds
add path is ok


在这里插入图片描述
如果你还想省去.jl,你还可以把run.jl中的宏代码再改一下:

macro run(file)
    return :( include($(string(file,".jl"))) )
end
julia> @run addPath
Now,is adding Julia working paths,and packages......
path arrangement and using package are finished!  costed 总共: 0.1809999942779541 seconds
add path is ok

更进一步,如果你想运行不同目录下的文件,又不想写相关的路径,在run.jl 文件中,你可以出大招:

下面考虑了几个问题:
第一,你的文件不用管在哪里,当然需要在加载的目录中;
第二,你可以省去.jl,会自动帮你补齐;
第三,如果你有.jl程序也能识别。
第四,如果文件路径或名字错误,会有提醒。
好了,已经把我暂时能想的已经想了。

macro run(file)
    _file = _get_file_(_align_file_(string(file)));
    if Base.Filesystem.isfile(_file)
        return :( include($_file)) 
    else
        return :( println("file path is wrong!",$file))
    end
end
# 判断是否需要补齐.jl
function _align_file_(file)
    str = string(file)
    split_string = split(str,'.');
    if last(split_string) == "jl" 
        return str
    else
        return string(str,".jl")
    end
end
# 自动找一下文件可能在的路径
function _get_file_(jlfile)
    path = ""
    for dir in Base.LOAD_PATH
        _path = Base.Filesystem.joinpath(dir,jlfile);
        if Base.Filesystem.isfile(_path) == true
            println("path : $_path");
            return _path
        end
    end
    _path = Base.Filesystem.joinpath(pwd(),jlfile)
    if Base.Filesystem.isfile(_path) ==true
        return _path
    end
    return path
end

如果,如果你还想再省一点的话,可以把run改成r。

@r addPath

不能再省了,再省可能就会出事了!

这样,你就可以更加简洁地在julia repl环境中运行程序了。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值