MOON
MOON

Reputation: 2801

How to install a series of Julia packages from a file

In Python, if I install mambaforge or conda then I can make a file with extension .yml and then inside it list the name of packages I want to install alongside their specific versions. How can I do a similar way of installing packages in Julia? I understand that if I have already installed Julia packages by addcommand in package manager, then I have a file named Project.toml which I can use to install the same packages later. However, this still does not look as good as Python's way of installing packages.

Upon further investigation I realized that to install Julia packages from an empty Prokect.tomlfile, I should add [deps]in the file followed by the name of packages I want and then give each package a uuidwhich can be found here. For example:

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

After all , this is still tedious as it needs to find all those uuids. How can I install packages in Julia the same way I described for Python?

Upvotes: 4

Views: 660

Answers (2)

Shayan
Shayan

Reputation: 6295

Maybe you can use this:

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"])

Upvotes: 0

Ashlin Harris
Ashlin Harris

Reputation: 422

Is there a particular reason that you want to write package names to a .yml file and then read the packages from there? After all, you can generate the Project file and add multiple dependencies automatically:

(@v1.8) pkg> generate MyProject # or whatever name you like

(@v1.8) pkg> activate MyProject

(MyProject) pkg> add Countries Crayons CSV # some example packages

(In recent versions of Julia, an installation prompt will appear if a package isn't already installed).

Speaking from experience, learning to use environments in Julia can be challenging to a new user, but rewarding! The documentation for Pkg.jl are helpful here.

If you are just assembling an environment for your own code, there is probably no need for you to manually edit Project.toml. On the other hand, if you are maintaining a package, you might wish to edit the file directly (e.g., for specifying compatability).

Upvotes: 1

Related Questions