Julia environments list

Starting a new Julia environment is very ease

julia>] activate test

(test) pkg> 

Is there a way to visualize a list of all the environments?

3 Likes

I recently found a file ~/.julia/logs/manifest_usage.toml which lists all recently used environments. I understand that this file is also used for ] gc.

4 Likes

Being able to list them all would be a nice feature. Basically, just parse that file, sort the keys and print them. Would be a nice addition to Pkg.

9 Likes

I arrived here looking for a way to list environments =)

julia> using Pkg

julia> Pkg.envdir()
"/home/jrun/data/.julia/environments"

shell> cd "/home/jrun/data/.julia"
/mnt/data/.julia

shell> ls -la
total 36
drwxr-xr-x  8 jrun jrun 6144 Aug  9 19:52 .
drwxr-xr-x  6 jrun jrun 6144 Aug  9 18:29 ..
drwxr-xr-x  5 jrun jrun 6144 Aug  9 19:51 artifacts
drwxr-xr-x  3 jrun jrun 6144 Aug  9 19:48 compiled
drwxr-xr-x  2 jrun jrun 6144 Aug  9 19:51 logs
drwxr-xr-x 16 jrun jrun 6144 Aug  9 19:51 packages
lrwxrwxrwx  1 jrun jrun   15 Aug  9 18:29 registries -> /tmp/registries
drwxr-xr-x  3 jrun jrun 6144 Aug  9 19:52 scratchspaces
drwxr-xr-x  3 jrun jrun 6144 Aug  9 18:29 servers

Looks like there is no env by default for the binary

Opened an issue https://github.com/JuliaLang/Pkg.jl/issues/2688

3 Likes

my workaround:

function list_Pkg_environments()
    env_dir = joinpath(Pkg.envdir(), string("v", VERSION)[1:end-2])
    # env_dir = Pkg.envdir()
    dir_list = readdir(env_dir, join= false)
    dir_list_full = readdir(env_dir, join= true)
    env_list = []
    for i_ = eachindex(dir_list)
        if isdir(dir_list_full[i_]) 
            println(dir_list[i_])
            push!(env_list, dir_list[i_]) 
        end
    end
    return env_list
end
1 Like

here is my function to select and activate a user defined Julia-environment:

function select_Pkg_environment(_environment::String="")
    curr_dir = pwd()
    env_dir = joinpath(Pkg.envdir(), string("v", VERSION)[1:end-2])
    # env_dir = Pkg.envdir()
    dir_list = readdir(env_dir, join= false)
    dir_list_full = readdir(env_dir, join= true)
    env_list = []
    for i_ = eachindex(dir_list)
        if isdir(dir_list_full[i_]) 
            # println(dir_list[i_])
            push!(env_list, dir_list[i_]) 
        end
    end
    if isempty(_environment)
        if isempty(env_list)
            println("No additional environments found!")
        else
            println("----   Number of available environments: ", length(env_list), "  ----")
            for i_ = eachindex(env_list)
                println(i_, ".) ",env_list[i_])
            end
            println("------------------------------------------------")
        end
    else
        if any(occursin.(_environment, string.(env_list)))
            if splitpath(Base.active_project())[end-1] != _environment
                cd(env_dir)
                Pkg.activate(_environment)
                cd(curr_dir)
                println("current dir: ", pwd())
            else
                @info(string("Environment \"", _environment, "\" is already loaded!"))
            end
        else
            @warn(string("Julia Pkg-environment: \"", _environment, "\" not found!"))
            @info("Call function without argument to list available environments.")
        end
    end
    return env_list
end

The strange thing is, that the command:

Pkg.activate("MyEnvironment")

opens a new user defined environment, if it is not yet known in a subfolder of Pkg.envdir().
And establishes the two environment files Project.toml & Manifest.toml in a sub-directory
with the name of the user defined environment as soon as a package is added via: Pkg.add("PackageName").
But the next time I give the identical command: Pkg.activate("MyEnvironment")
Pkg wants to establish a new environment definition directory at the current directory pwd().
Is this a bug or a feature?

1 Like

And here my code to establish a new environment in a sub-folder of Pkg.envdir():

function MyLib_Add_Pkg_env(_environment::String="")
    curr_dir = pwd()
    env_dir = joinpath(Pkg.envdir(), string("v", VERSION)[1:end-2])
    # env_dir = Pkg.envdir()
    dir_list = readdir(env_dir, join= false)
    dir_list_full = readdir(env_dir, join= true)
    env_list = []
    for i_ = eachindex(dir_list)
        if isdir(dir_list_full[i_]) 
            # println(dir_list[i_])
            push!(env_list, dir_list[i_]) 
        end
    end
    if isempty(_environment) ||  ~any(occursin.(_environment, string.(env_list)))
        cd(env_dir)
        Pkg.activate(_environment)
        Pkg.add("Revise")
        cd(curr_dir)
        println("current dir: ", pwd())
    end
    return env_list
end

I have updated / debugged my two functions:

function MyLib_add_primary_env(_environment::String="")
    Pkg.activate() # make sure we are in the primary default environment        
    env_dir, _ = splitdir(Base.active_project())
    # env_dir = Pkg.envdir()
    dir_list = readdir(env_dir, join= false)
    dir_list_full = readdir(env_dir, join= true)
    env_list = []
    for i_ = eachindex(dir_list)
        if isdir(dir_list_full[i_]) 
            # println(dir_list[i_])
            push!(env_list, dir_list[i_]) 
        end
    end
    if isempty(_environment) ||  ~any(occursin.(_environment, string.(env_list)))
        prim_env_dir = joinpath(env_dir, _environment)
        Pkg.activate(prim_env_dir)
        Pkg.instantiate()
    end
    return env_list
end

and:

function MyLib_select_primary_env(_environment::String="")
    b_dbg = false
    prim_env_dir = []
    if VERSION < VersionNumber(1, 6, 7)
        error("Julia too old! Please upgrade to v > v1.6.7!")
    else
        Pkg.activate() # make sure we are in the primary default environment        
        env_dir, _ = splitdir(Base.active_project())
    end
    # env_dir = Pkg.envdir()
    dir_list = readdir(env_dir, join= false)
    dir_list_full = readdir(env_dir, join= true)
    env_list = []
    for i_ = eachindex(dir_list)
        if isdir(dir_list_full[i_]) 
            # println(dir_list[i_])
            push!(env_list, dir_list[i_]) 
        end
    end
    if isempty(_environment)
        Pkg.activate()
        if isempty(env_list)
            println("No additional environments found!")
        else
            if b_dbg
                println("----   Number of available environments: ", length(env_list), "  ----")
                for i_ = eachindex(env_list)
                    println(i_, ".) ",env_list[i_])
                end
                println("------------------------------------------------")
            end
        end
    else
        prim_env_dir = joinpath(env_dir, _environment)
        if any(occursin.(_environment, string.(env_list)))
            if splitpath(Base.active_project())[end-1] != _environment
                Pkg.activate(prim_env_dir)
            else
                @info(string("Environment \"", _environment, "\" is already loaded!"))
            end
        else
            @warn(string("Julia Pkg-environment: \"", _environment, "\" not found!"))
            @info("Call function without argument to list available environments.")
        end
    end
    return env_list
end