Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

package - 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.

question from:https://stackoverflow.com/questions/65858895/julia-if-i-activate-a-new-environment-why-can-i-still-load-modules-from-my-ge

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...