How to add dependencies to my Julia package?
Asked Answered
T

2

13

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.

Tessin answered 26/9, 2020 at 14:16 Comment(0)
I
15

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"
Imbecility answered 26/9, 2020 at 14:22 Comment(3)
But this is entering the package REPL mode for the general "pkg" i want it to be for my specific package, say HelloWorld >Tessin
I added some details regarding environment activation. Is this what you needed?Vitrescent
I want to use a python package in my Julia package. What's the procedure for adding a python package as a dependency?Discoid
N
2

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")
Newmint answered 17/8, 2022 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.