Julia is missing the deactivate command

Thanks for the link. :+1: I’ll have to read that a few times.

Base.active_project() returns the path to a Project.toml even at startup, but that’s not quite the same as activating project… subtle.

Instead of trying to guess where Pkg.activate() will take me, I think I’d rather be explicit with something like:

cd("path/to/toml")
import Pkg
Pkg.activate(".")
Base.active_project()  # check you're where you think you are

Thanks again Fredrik

I think you’re looking for what was written here, you just need to type
activate
(with no argument) in package mode.

By default the load path is

julia> Base.LOAD_PATH
3-element Vector{String}:
 "@"
 "@v#.#"
 "@stdlib"

where @ is essentially a ā€œpointerā€ to an active project. By default it doesn’t point to anything, it exands to nothing

julia> Base.load_path()
2-element Vector{String}:
 "/home/fredrik/.julia/environments/v1.6/Project.toml" # expansion of @v#.# in LOAD_PATH
 "/opt/julia/julia-1.6.1/share/julia/stdlib/v1.6"      # expansion of @stdlib in LOAD_PATH

If you Pkg.activate something you set the pointer to the thing you activated

julia> using Pkg; Pkg.activate("/tmp")

julia> Base.load_path()
3-element Vector{String}:
 "/tmp/Project.toml"
 "/home/fredrik/.julia/environments/v1.6/Project.toml"
 "/opt/julia/julia-1.6.1/share/julia/stdlib/v1.6"

and then Pkg.activate() (no argument) sets the pointer to ā€œnullā€ again:

julia> Pkg.activate()

julia> Base.load_path()
2-element Vector{String}:
 "/home/fredrik/.julia/environments/v1.6/Project.toml"
 "/opt/julia/julia-1.6.1/share/julia/stdlib/v1.6"

The --project command line flag, and the JULIA_PROJECT environment variable are other ways to set the pointer, similar to Pkg.activate.

Thanks magister-ludi. Well I’m not so clear about the analogy with cd. It sounds as if once a project has been activated, the only way to deactivate it is to activate another project. I’m not sure I understand it.

Thanks Fredrik, it’s much clearer now. I was confused when Base.active_project() was not returning a null pointer, but if you say it’s null I’ll take your word for it. :+1:

I think the analogy is that in UNIX-like systems, cd without an argument takes you back to a a kind of ā€˜base state’, which is the home directory. So activate without an argument takes you back to the ā€˜no specific package loaded’ state.

Edit: I mean no specific package activated.

yes, thanks. I was just not sure if that ā€˜base state’ was considered active or not. :sweat_smile:

I came here looking for deactivate()

status remark for v1.7.0:
I found confusing information regarding the environment variable ā€œJULIA_LOAD_PATHā€.
On the page about environment variables it is stated:

Unlike the shell PATH variable, empty entries in JULIA_LOAD_PATH are expanded to the default value of LOAD_PATH , ["@", "@v#.#", "@stdlib"] when populating LOAD_PATH . This allows easy appending, prepending, etc. of the load path value in shell scripts regardless of whether JULIA_LOAD_PATH is already set or not. For example, to prepend the directory /foo/bar to LOAD_PATH just do.

But under windows I have the impression that this is not true. Here is what I have added in my startup.jl:

s_modul_path = raw"C:\data\julia\MyLib"
ENV["JULIA_LOAD_PATH"] = s_modul_path

The startup file is executed at start up, I can see the welcome massage that is included.
My expectation was, that modules that are located in this folder will be found,
unfortunately this is not the case.

  • JULIA_LOAD_PATH != JULIA_DEPOT_PATH
  • Once Julia is started, both environment variables have been processed and changing them won’t have any effect.
  • To modifiy the load or depot path (stack) you need to modify the LOAD_PATH or DEPOT_PATH variables, respectively.

So I’d try something like push!(LOAD_PATH, s_modul_path).

Yes, this works fine. In fact this is the method I use. :slight_smile:
But I wonder, if the information about JULIA_LOAD_PATH is correct.
The following does not work:

ENV["JULIA_LOAD_PATH"] = string("C:\\data\\julia\\MyLib", ";", ENV["JULIA_LOAD_PATH"])

I hope this is the correct translation of the unix example:

export JULIA_LOAD_PATH="/foo/bar:$JULIA_LOAD_PATH"

The error message is:

LoadError: KeyError: key "JULIA_LOAD_PATH" not found

It is not set by default, so you need something like

ENV["JULIA_LOAD_PATH"] = string("C:\\data\\julia\\MyLib", ";", get(ENV, "JULIA_LOAD_PATH", ""))

to mimic the shell behavior of just inserting an empty string for missing variables. (However, as noted above it is too late to change this once already in Julia.)

You need to set the environment variable before starting Julia. Once you’re in Julia you can just manipulate the contents of LOAD_PATH directly.