How to programmatically identify the currently active environment?

When in Pkg mode, the prompt shows the name of the currently active environment. I would like to obtain this information programmatically. As far as I can tell, Pkg doesn’t document any way to do this.

From poking around in the source code I figured out that Pkg.Types.Context().env.pkg.name will return the name of the currently active package. But it doesn’t work if the environment is just the main Julia environement instead of a package.

Is there a better way to programmatically identify the currently active environment?

2 Likes

Maybe try Base.active_project()

7 Likes

That seems to work. Thanks

Can I programmatically find out if Pkg.instantiate() is needed?

Interestingly this can take quite some time (5 seconds) even when there is (seemingly) nothing to do.

julia> using Pkg

(RunamoLotteryModel) pkg> st
     Project Rv0.1.0
      Status `C:\Users\bernhard.konig\Documents\Repositories\R.jl\Project.toml`
  [336ed68f] CSV v0.8.5
  [1b08a953] Dash v0.1.6
  [1b08a953] DashCoreComponents v1.17.1
  [1b08a953] DashHtmlComponents v1.1.4
  [1b08a953] DashTable v4.12.0
  [a93c6f00] DataFrames v1.2.2
  [31c24e10] Distributions v0.25.16
  [ff294ebf] ExcelWriter v0.1.7 `https://github.com/kafisatz/ExcelWriter.jl/#master`
  [0f8b85d8] JSON3 v1.9.1
  [b9914132] JSONTables v1.0.1
  [5078a376] LazyArrays v0.21.20
  [69de0a69] Parsers v1.1.2 ⚲
  [92933f4c] ProgressMeter v1.7.1
  [e6cf234a] RandomNumbers v1.5.3
  [295af30f] Revise v3.1.19
  [2913bbd2] StatsBase v0.33.10
  [3a884ed6] UnPack v1.0.2
  [2a0f44e3] Base64
  [ade2ca70] Dates
  [8ba89e20] Distributed
  [b77e0a4c] InteractiveUtils
  [44cfe95a] Pkg
  [9a3f8284] Random
  [10745b16] Statistics
  [8dfed614] Test

julia> @time Pkg.instantiate()
  5.568895 seconds (6.03 M allocations: 378.258 MiB, 1.12% gc time, 20.15% compilation time)


1 Like

and here is my command to retrieve the current environment name:

splitpath(Base.active_project())[end-1]