在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环境中运行程序了。