math_lover
math_lover

Reputation: 956

How to add dependencies to my Julia package?

I have created a package (let's call it package_name) in Julia; the file structure is already generated along with the Project.toml and Manifest.toml files and I have already added some dependencies when I created the package.

I forgot to add a dependency and I would like to get the REPL to show:

(package_name) pkg > 

so that I may type

add dependency_name

How do I get the REPL to show this? I think I need to go to the package folder and (re) activate the package but I am unable to navigate to it with cd.

Showing the exact commands I should type in the REPL would be helpful.

Upvotes: 13

Views: 5964

Answers (2)

Shep Bryan
Shep Bryan

Reputation: 669

You can also add deps manually to your project.toml file like

name = "MyPackage"
uuid = "e91191c6-8984-4a4d-b031-ef6fb65a77ca"
authors = ["Shep Bryan IV and contributors"]
version = "0.1.0"

[deps]
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

This requires that you be able to find the corresponding uuid codes for each package you use. Currently there is no simple function to do this, but if you really want to do it manually you can find a complete list of uuids for registered packages here.

Alternatively you can use this crude script to get the uuids from within Julia:

using Downloads

function get_uuid_from_registry(modulename)
    # Download the registry to temporary file
    tempfile = Downloads.download("https://github.com/JuliaRegistries/General/blob/master/Registry.toml")
    # Open the tempfile
    f = open(tempfile, "r")
    # Loop through the lines in the file
    for line in readlines(f)
        if occursin(">$(modulename)<", line)
            # Cut out uuid from HTML file
            uuid = split(split(line, "<span class=\"pl-smi\">")[2], "</span>")[1]
            return uuid
        end
    end
    println("No module found")
    return
end

uuid = get_uuid_from_registry("HDF5")

Upvotes: 2

In order to get the package REPL mode, you should type a closing bracket ] when your cursor is at the beginning of the line. Likewise, when in package REPL mode, you need to type BackSpc right after the prompt in order to get back to standard REPL mode:

julia> # type ] here to enter the Pkg REPL

# We're now in the Pkg REPL, but the default environment is active
# Let's activate the environment we want
# (replace the path below with "." to activate the environment defined in the current working directory)
(@v1.5) pkg> activate /path/to/package

# Now we see that the correct environment is active
# This is where new dependencies will be added
(package_name) pkg> add DepName

(package_name) pkg> # type BackSpace here to get back to the standard REPL

julia>

Additionally, you could achieve the same thing without entering the Pkg REPL mode, by using the pkg"..." string macro defined in the Pkg library:

julia> using Pkg

julia> pkg"activate /path/to/package"

julia> pkg"add DepName"

Upvotes: 15

Related Questions