如何从一个文件安装一系列Julia包



在Python中,如果我安装mambaforgeconda,那么我可以创建一个扩展名为.yml的文件,然后在其中列出我想要安装的包的名称以及它们的特定版本。如何在Julia中以类似的方式安装包?我明白,如果我已经在包管理器中通过add命令安装了Julia包,那么我有一个名为Project.toml的文件,我可以在以后使用它来安装相同的包。然而,这看起来仍然不如Python安装包的方式好。

经过进一步的调查,我意识到要从一个空的Prokect.toml文件安装Julia包,我应该在文件中添加[deps],后面跟着我想要的包的名称,然后给每个包一个uuid,可以在这里找到。例如:

[deps]
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"

毕竟,这仍然是乏味的,因为它需要找到所有的uuid。我如何在Julia中安装包,就像我在Python中描述的那样?

您想要将包名称写入.yml文件然后从那里读取包是否有特殊原因?毕竟,您可以自动generate项目文件和add多个依赖项:

(@v1.8) pkg> generate MyProject # or whatever name you like
(@v1.8) pkg> activate MyProject
(MyProject) pkg> add Countries Crayons CSV # some example packages

(在最近的Julia版本中,如果软件包尚未安装,将出现安装提示)。

从经验来看,学习使用Julia中的环境对新用户来说可能是一个挑战,但却是有益的!Pkg.jl的文档在这里很有帮助。

如果您只是为自己的代码组装一个环境,可能不需要手动编辑Project.toml。另一方面,如果您正在维护一个包,您可能希望直接编辑该文件(例如,为了指定兼容性)。

也许你可以这样做:

using TOML
using HTTP
using DataStructures
## Get the Registry.toml file if it already doesn't exist in the current directory
function raed_reg()
if !isfile("Registry.toml")
HTTP.download(
"https://raw.githubusercontent.com/JuliaRegistries/General/master/Registry.toml",
"Registry.toml"
)
end
reg = TOML.parsefile("Registry.toml")["packages"]
return reg
end
## Find the UUID of a specific package from the Registry.toml file
function get_uuid(pkg_name)
reg = raed_reg()
for (uuid, pkg) in reg
if pkg["name"] == pkg_name
return uuid
end
end
end

## Create a dictionary for the gotten UUIDs, by setting the name = UUID and convert it to a project.toml file
function create_project_toml(Pkgs::Vector{String})
reg = raed_reg()
pkgs_names_uuid = OrderedDict{AbstractString, AbstractString}()
pkgs_names_uuid["[deps]"] = ""
for pkg in Pkgs
_uuid = get_uuid(pkg)
pkgs_names_uuid[pkg] = _uuid
end
open("project.toml", "w") do io
TOML.print(io, pkgs_names_uuid)
end
end
## Test on the packages "ClusterAnalysis" and "EvoTrees"
create_project_toml(["ClusterAnalysis", "EvoTrees"])

最新更新