Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - If I activate a new environment, why can I still load modules from my general environment?

I was wondering how the environments in Julia work. I thought that by creating a new environment you should be able to see only the packages that are in that environment, but that doesn't seem to be the case. Why is that so?

I.e. if I create a temporary directory and start Julia with that directory as an environment, I still can load modules that are in my general environment, but not in the standard library.

$ mkdir /tmp/jl_temp
$ julia --project=/tmp/jl_temp 
(jl_temp) pkg> st
Status `/tmp/jl_temp/Project.toml` (empty project)

julia> using Plots

julia> plot()

This works without giving me any package not installed error. Can someone explain me the reason of this behavior? To me it should not be able to load any package except for the standard library and the packages inside the Project.toml file of the environment. I could not find any answer in the Documentation of Pkg.jl.

like image 623
Oskar Avatar asked Jan 23 '21 11:01

Oskar


1 Answers

This is managed by how you set the LOAD_PATH variable. See here.

By default LOAD_PATH is set to:

julia> LOAD_PATH
3-element Array{String,1}:
 "@"
 "@v#.#"
 "@stdlib"

A full search path is checked in the following way:

(@v1.5) pkg> activate .
 Activating new environment at `~/Project.toml`

julia> Base.load_path()
3-element Array{String,1}:
 "/home/bkamins/Project.toml"
 "/home/bkamins/.julia/environments/v1.5/Project.toml"
 "/home/bkamins/julia/share/julia/stdlib/v1.5"

(I have activated project in my current working directory)

So you can see that if some package is not found in your project Julia falls back to the next entries of LOAD_PATH. If you want to avoid it then modify LOAD_PATH as needed.

like image 124
Bogumił Kamiński Avatar answered Nov 04 '22 05:11

Bogumił Kamiński