How to import/add all packages in a julia project? - julia

When I download a Julia library and try to execute the tests it gives the following message:
Got exception outside of a #test
LoadError: ArgumentError: Package OrdinaryDiffEq not found in current path:
- Run import Pkg; Pkg.add("OrdinaryDiffEq") to install the OrdinaryDiffEq package.
After that I run the required commands and the dependency gets installed. Now when I run the tests again it will show the same message just with another dependency.
Is there a way to add all of the Packages at once?

You can activate the test folder of that package (using Pkg; Pkg.activate("/that/path) and then run Pkg.instantiate().
This will download and build the pkg that you may missing and make you in the conditions to run the test.
Note: this should work for newer pkgs that use a separate Project.toml file for testing, not sure for old packages that still specify the dependencies used for test as a subsection of the main pkg Project.toml file.

Related

Julia ArgumentError: Package RCall not found in current path:

I just opened a new Jupyter notebook, and want to load some libraries.
using LinearAlgebra
using SparseArrays
using Statistics
using StatsBase
using RCall
Everytime I run the cell, I keep on getting the same error:
ArgumentError: Package StatsBase not found in current path:
- Run `import Pkg; Pkg.add("StatsBase")` to install the StatsBase package.
So I run the suggested commands:
import Pkg; Pkg.add("StatsBase")
I rerun the top cell, and I get a new error:
ArgumentError: Package RCall not found in current path:
- Run `import Pkg; Pkg.add("RCall")` to install the RCall package.
What I don't get is why I get these error messages, as have installed these packages previously. If I open one of my pre-existing Jupyter notebooks, whis in a different directory, this error does not occur. However, if I run Julia in terminal I get the same error. Seemingly it is working in some directories, and not others.
What may be causing the problem?
Julia can have many virtual environments. The Julia virtual environment has its own package installation state, independent from the global environment.
Contrary to Python, each environment just keeps links to a package repository in local hard drive rather than contain full copies of each package.
The environment is defined in the Project.toml file.
Simply run Pkg.status() to check which environment you are working currently with (this time it shows my global environment):
julia> Pkg.status()
Status `C:\JuliaPkg\Julia-1.6.1\environments\v1.6\Project.toml`
[6e4b80f9] BenchmarkTools v1.1.0
[1e616198] COSMO v0.8.1
[336ed68f] CSV v0.8.5
...
For Jupyter notebooks those are defined in the Project.toml that is located at the same folder as your notebook. This is most likely the source of your problems and the reason why you observe "strange" package installation states.
The Procet.toml file is normally created when you activate a folder:
julia> using Pkg
julia> Pkg.activate(".")
Activating new environment at `C:\SomeMyFolder\Project.toml`
However, when you run Jupyter (e.g. notebook(dir=".")) Pkg.activate(".") happens automatically where a Project.toml file is found in the current folder.

Cannot install any package in Julia

I had used Julia some months back for a project, and didn't use it since. I hadn't faced problems back then. But now, I absolutely am not able to install any package.
(#v1.5) pkg> add Distributions
Resolving package versions...
ERROR: Unsatisfiable requirements detected for package Distributions [31c24e10]:
Distributions [31c24e10] log:
├─Distributions [31c24e10] has no known versions!
└─restricted to versions * by an explicit requirement — no versions left
Any package I install, I am getting the same error. How do I fix it?
This is the status of Pkg:
(#v1.5) pkg> status
Status `C:\Users\jaine\.julia\environments\v1.5\Project.toml`
[6e4b80f9] BenchmarkTools v0.5.0
[7073ff75] IJulia v1.21.2
[91a5bcdd] Plots v1.6.0
[438e738f] PyCall v1.91.4
[d330b81b] PyPlot v2.9.0
[2913bbd2] StatsBase v0.33.0
Given
├─Distributions [31c24e10] has no known versions!
it looks like you have a missing or corrupt package registry. The package manager will reinstall it for you if you delete .julia/registries and try to add the package again.
You can do this from within julia
rm(joinpath(homedir(), ".julia", "registries"), recursive=true, force=true)
These are indeed some of the more tricky situations to resolve so please bear with me as we work through this.
If you are not familiar with the basics of the package manager, it's worth a quick read to check out: https://julialang.github.io/Pkg.jl/v1/getting-started/
Assuming you are now in the Julia Repl, my general work flow for these issues is to remove everything it warns me about. The core problem here is that one of your existing packages is saying it needs a specific version of Distributions.jl but it's not able to add it. So, the first step would be to enter the pkg manager by doing ] and type rm Distributions.
Then, while still in the pkg mode, do resolve and add Distributions.
So the process is to just remove each package the Repl complains about until it stops complaining.
Note: If you run into something like this again, the worst-case scenario is to remove the project and manifest files for the specific Julia version you are using. If you navigate to ".julia/environments" you should be able to completely reset the Julia Env for a specific version there (that way you don't need to actually reinstall Julia).

How to download and setup dependencies for a Julia project that is in development?

I am trying to download and install dependencies for a Julia project that's not in the package registry. It has a manifest and project file. How do I get all of the packages it depends on to download at once using the Julia Package manager?
Download the source: git clone https://github.com/RandomUser/Unregistered.jl
Activate the project: pkg> activate Unregistered.jl
Ensure any dependencies are installed: pkg> instantiate
Once the package is set up, you can use the package normally.
You can load the package:
julia> using Unregistered
Or even run its test suite:
pkg> test
FWIW, here is a "pure" Julia version of what #David Varela suggested.
After substituting <url-to-project> and /some/local/path this "just works" in the REPL or similar:
using Pkg
Pkg.GitTools.clone("<url-to-project>", "/some/local/path")
cd("/some/local/path")
Pkg.activate(".")
Pkg.instantiate()
# Pkg.precompile() # optional
Preparation (optional):
Create a new folder somewhere and cd into it.
Start Julia with julia --project=.
Now the actual downloading/installing:
Develop the project locally: pkg> dev --local https://github.com/RandomUser/Unregistered.jl
This will clone the unregistered project into a local subfolder dev/Unregistered and will install all the required dependencies.
If the unregistered project is a Julia package, you can now simply using Unregistered. If you want to work on Unregistered.jl itself you can pkg> activate dev/Unregistered to work in the project environment.

How to use R CMD Install without dependencies check?

I'm running R CMD INSTALL --build package on a windows computer. My package imports a couple of other packages which themselves depend on some more packages. I have all dependencies installed in the local r_libs folder and everything works.
Now sometimes I have the my package source code on a different windows computer. On this computer I don't have all the dependency packages installed.
When I try to use R CMD INSTALL --build package, I get the obvious "ERROR: dependencies 'package a', 'package b', etc, are not available for package".
My question is: Can I build the package using R CMD INSTALL --build without the dependency checks and without removing the Import and Depends entries in the DESCRIPTION file?
After consulting --help, I tried the --no-test-load option but no luck.
I reckon you want to build a .zip binary version of the package on a computer where not all dependencies are installed. And I'm afraid I'll have to disappoint you, as this won't be possible.
Building a binary package is done in two steps: first the package is installed from source (that's why you have to use R CMD INSTALL and then the created binaries are zipped in a convenient format for installation on a windows machine. The dependencies are checked at time of installation from source, and any missing dependencies will throw the error you're facing.
As R needs information from the dependencies at time of installation from source, you can't get around installing them before building the whole thing. This also makes sense. An installed package in R contains a set of .rds files which contain package information in a more convenient format for R. In order to create that information for the NAMESPACE file, it needs to be able to access the packages from which functions are imported. If not, it can't construct the correct information about the namespace.
So your only option is to install the dependencies on the computer you use to build. And if you actually want to use the package on that computer, you'll have to install those dependencies anyway.
More information:
R Internals : https://cran.r-project.org/doc/manuals/r-release/R-ints.html#Package-Structure
Writing R Extensions: https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-namespaces

Building an R package with a pre-compiled shared library

I am facing a problem with an R package that I am writing and trying to build with a pre-compiled shared library. Let me try to briefly describe the problem:
this package (let's call it mypack) relies on a shared library mylib.dll that is already compiled and that I cannot compile on the fly while building the R package.
the library mylib.dll has been compiled on a x64 machine under Windows and can be loaded in R with dyn.load.
the package contains the required file NAMESPACE, where useDynLib(mylib.dll) is specified. The function .onLoad containing the instruction library.dynam('mylib.dll', pkg, lib) is also specified in a file zzz.R.
the R package mypack is built with Rtools using the usual command Rcmd INSTALL, and I then add a directory libs where I save mylib.dll.
when I try to load the package in R with library(mypack), I get the following error message:
Error: package 'mypack' is not installed for 'arch=x64'
This is puzzling me. Why can the shared library be loaded smoothly in R, but when I build a package using it I am getting this weird error message?
Thank you very much in advance for your help!
That error message comes from this code in library:
if (nzchar(r_arch) && file.exists(file.path(pkgpath,
"libs")) && !file.exists(file.path(pkgpath, "libs",
r_arch)))
stop(gettextf("package %s is not installed for 'arch=%s'",
sQuote(pkgname), r_arch), call. = FALSE, domain = NA)
which is telling me you need a {package}/libs/{arch} folder in your built package (ie the installed directory) with an {arch} that matches your system's arch, as given by r_arch <- .Platform$r_arch
I'm guessing your build has failed to make this correctly. Is there any C code in your source?

Resources