# How to programmatically identify the currently active environment?

**URL:** https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257
**Category:** General Usage
**Tags:** package, environment
**Created:** [July 17, 2020, 7:56pm UTC](https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257 "2020-07-17T19:56:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)
#### Post date: [July 17, 2020, 7:56pm UTC](https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257/1 "2020-07-17T19:56:07Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [July 17, 2020, 7:58pm UTC](https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257/2 "2020-07-17T19:58:23Z")

</div>

Maybe try `Base.active_project()`

---

<div class="post-metadata">

### Author: ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)
#### Post date: [July 17, 2020, 8:15pm UTC](https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257/3 "2020-07-17T20:15:15Z")

</div>

That seems to work. Thanks

---

<div class="post-metadata">

### Author: ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)
#### Post date: [September 17, 2021, 2:06pm UTC](https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257/4 "2021-09-17T14:06:49Z")

</div>

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
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)

```

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [November 24, 2022, 1:02pm UTC](https://discourse.julialang.org/t/how-to-programmatically-identify-the-currently-active-environment/43257/5 "2022-11-24T13:02:13Z")

</div>

> [@dilumaluthge](#):
>
> Base.active\_project()

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

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

```
