Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro to add "using" packages automatically #2027

Closed
mossr opened this issue Sep 20, 2020 · 8 comments
Closed

Macro to add "using" packages automatically #2027

mossr opened this issue Sep 20, 2020 · 8 comments

Comments

@mossr
Copy link

mossr commented Sep 20, 2020

Not sure if this is where you'd like the Pkg manager to go, but I threw together an @add macro to automatically install missing packages when calling using. Example:

@add using Clustering

This will run using Clustering if we already have it, otherwise it will add it, then run using Clustering.

Here's the macro:

macro add(expr)
    packages::Vector{Symbol} = [arg.args[end] for arg in expr.args]
    quote
        for package in $(esc(packages))
            using_package = Meta.parse("using $package")
            try
                eval(using_package)
            catch err
                if err isa ArgumentError
                    using Pkg
                    Pkg.add(string(package))
                    eval(using_package)
                else
                    throw(err)
                end
            end
        end
    end
end

This also works for multiple comma separated packages:

@add using Random, Clustering, LinearAlgebra

I tried searching for related issues to no avail. My use-case is for one-off lecture-style Pluto notebooks given to students so I can just call a one-line @add using Package instead of wrapping everything in a try/catch or creating a light-weight package with a Project.toml just for one file. Thoughts and feedback welcome!

@fredrikekre
Copy link
Member

See JuliaLang/julia#26469, JuliaLang/julia#35062

@mossr
Copy link
Author

mossr commented Sep 21, 2020

Thank you for linking those (I knew this wasn't a unique issue but failed to find those).

I created a separate package—AddPackage.jl—with this macro so I can use it within Pluto notebooks sent to students. Also, the threads you linked seem to focus on the interactive REPL responses when calling using Package, where @add using Package is helpful for notebooks or single one-off scripts without a Project.toml.

@fredrikekre
Copy link
Member

Everything has a Project.toml file, but perhaps Pluto can't handle the interactivity?

@mossr
Copy link
Author

mossr commented Sep 21, 2020

Pluto can handle adding packages within a cell, but the use-case is mainly for convenience so cells don't look like:

try
    using Package
catch
    using Pkg
    Pkg.add("Package")
    using Package
end

and instead look like:

@add using Package

(again, for lecture-style notebooks/Julia files that may be sent as single files and not part of a package, thus without a Project.toml)

@fredrikekre
Copy link
Member

I mean that if something like JuliaLang/julia#26469 were implemented.

@mossr
Copy link
Author

mossr commented Sep 21, 2020

Good point, then I agree that Pluto may not be able to handle that level of interactivity.

@StefanKarpinski
Copy link
Member

I originally wanted to prompt to add packages when someone does using and the package isn't already added. I still think that would be a nice feature to have.

@oxinabox
Copy link
Contributor

oxinabox commented Jul 14, 2021

Effectively closed by JuliaLang/julia#39026
and probably by Pluto changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants