Julia ArgumentError: Package RCall not found in current path:

Viewed 1464

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?

1 Answers

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.

Related