Using package / install automatically

Hi,
If I do using DataFrames and the package is not install, Julia 1.7 will ask me if I want to install it and I will have to type y or n.
Is there a way to skip that step and have Julia installed the package if it is not available?
Of course one can write a function that check if the package is installed or not and then install it.
I was wondering if there is an option I can pass to using DataFrames that will automatically install the package if not installed?
Could that be a feature request to be added directly in base Julia?
Thank you

2 Likes

in case you don’t know this, you can just hit enter without typing anything and it will install it (i.e. it defaults to y)

1 Like

What you could do, while not good form, is e.g.:

julia> using Pkg; @sync Pkg.add("Air"; preserve=PRESERVE_ALL); using Air

but confusingly I got asked the question pressed “n” and then the package Air.jl was installed anyway! It seemed strange, then I figured out what might be happening. I think Pkg.add runs but is too slow, not finishing and then using Air ran out of order, provides the prompt (would only happen in the REPL), doesn’t install and then Pkg.add finishes, that doesn’t ask. I first tried without @sync that I thought might fix the problem.

That was in Julia 1.7 RC, in Julia 1.6 the behavior is different so this might be a bug exposed by new capability (using was supposed to by multi-threaded, and may be in my version but that was reverted postponed to 1.8). This seems avoided if using Air is on a separate line.

The reason to NOT do this, is that the package might not install anyway. Sometimes no version is compatible with the packages you have already installed, with none of the options even PRESERVE_NONE. This is also slow when it downloads (no more than usual) but might also be when nothing needs to be installed.

If you know you start with an empty (or known) environment, you might get away with providing specify exact version= … but thinking about it, it might not be needed.

julia> Pkg.offline(false)

julia> @time Pkg.add("Air"; preserve=PRESERVE_ALL);
  Resolving package versions...
No Changes to `~/.julia/environments/v1.6/Project.toml`
No Changes to `~/.julia/environments/v1.6/Manifest.toml`
  0.657200 seconds (2.26 M allocations: 172.410 MiB, 19.45% gc time)

There’s no quiet option for Pkg.add, unless you hack Julia or strictly the Pkg, but there IS available something to redirect (std) output, that might be possible to use to make the text disappear.

Thank you for the feebacks.
Hmm, I guess right now the correct way to do it is to check if the package is installed. If not installed, then add it and load it.
It would have been nice to have a option when using using to install it automatically without having to answer that question.

It will likely never be an option for using, provided by Julia (as opposed to a functional equivalent in a package). I know of no language doing similar (at work, our home-made programming language does connect to the internet, or internal host behind firewall, if I recall to add stuff, and file Git issues for some errors), and you could be behind a firewall. This will be brittle, but you could do as I suggested, and someone could provide a package, with a macro, e.g. @auto_download using your_package. I doubt it’s high priority for anyone to provide it, or suppress output of Pkg.add, but as I wrote I believe it’s possible already without changing Pkg.add.

1 Like

There could be an option, say en environment variable, to automatically accept the auto-install. I think it is fine opening an issue about it on the Pkg.jl repo so it can be discussed.

1 Like