# Detect if current "package" has been activated

**URL:** <https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764>\
**Category:** General Usage\
**Tags:** pkg\
**Created:** [July 25, 2019, 12:36am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764 "2019-07-25T00:36:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 25, 2019, 12:36am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/1 "2019-07-25T00:36:56Z")

</div>

I’m not sure I’m doing this correctly but basically I’m building an “application” not a module. I have a Manifest.toml and Project.toml defined that contain the modules this application uses. So my initial julia file does an:

```julia
import Pkg
Pkg.activate(@ __DIR__ )
Pkg.instantiate();

```

To ensure that the modules I need are available for the `using` or `import` commands. My annoyance is that the `Pkg.instantiate()` call takes a bit of time to execute. Which is annoying during development, since I’m in an interactive session and the package is already instantiated from the last time “executed” the file.

What I would like to do is check if I’m already instantiated and NOT execute the command in that situation. I was hopeful that I could use `Pkg.installed()` for that, but in a clean instance it reports the modules in the base (not sure if there is a better name) environment.

Is there a way to detect the first run vs subsequent runs of a file under Atom/Juno? Or maybe there is a better way to bring in the modules I need?

What I figured right after writing this was doing:

```julia
try
    if LOADED
        println("Modules are loaded..")
    end
catch ex
    import Pkg
    Pkg.activate(@ __DIR__ )
    Pkg.instantiate();
    global LOADED = true
end

```

which might be using a hammer to crack a nut…

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 25, 2019, 12:42am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/2 "2019-07-25T00:42:40Z")

</div>

`instantiate` should be a no-op if everything is already downloaded.

Anyway, you can check if `Base.active_project() == dirname(@ __DIR__ )`

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 25, 2019, 12:44am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/3 "2019-07-25T00:44:54Z")

</div>

Oh, nice, thanks…I think the delay with instantiate is that it updates the repositories every time it’s called:

```julia
julia> Pkg.instantiate()
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`

```

At least for me.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 25, 2019, 12:46am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/4 "2019-07-25T00:46:22Z")

</div>

Ah, we should fix that.

Edit: [https://github.com/JuliaLang/Pkg.jl/pull/1264](https://github.com/JuliaLang/Pkg.jl/pull/1264)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 25, 2019, 6:20am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/5 "2019-07-25T06:20:59Z")

</div>

I agree. That takes a bit of time.

What I find useful is to avoid the call to `Pkg.instantiate();`. I use it when restarting Julia, with no ill perceived effect at all. I’m not sure if it also works when Julia was running in a different environment before, but it certainly seems to work when Julia was just restarted to the same environment in which it was running before.

Is that expected? When is `Pkg.instantiate();` absolutely required? (@fredrikekre)

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 25, 2019, 12:11pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/6 "2019-07-25T12:11:04Z")

</div>

> [@PetrKryslUCSD](#):
>
> When is `Pkg.instantiate();` absolutely required?

When packages are missing (missing as in not downloaded).

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 26, 2019, 3:38am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/7 "2019-07-26T03:38:00Z")

</div>

Excellent. `activate` by itself is quick.  
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:** [July 7, 2020, 1:45pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/8 "2020-07-07T13:45:57Z")

</div>

I am wondering why instantiate is not called automatically. Julia does ‘notice’ when instantiate is necessary after all.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 7, 2020, 3:51pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/9 "2020-07-07T15:51:52Z")

</div>

I don’t know what the rationale for the people who designed it is, but `instantiate` can be a pretty involved computational action, and interrupting in the middle is not a great idea. I wouldn’t want that to happen without explicit request 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 7, 2020, 5:43pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/10 "2020-07-07T17:43:33Z")

</div>

> [@bernhard](#):
>
> I am wondering why instantiate is not called automatically. Julia does ‘notice’ when instantiate is necessary after all.

> [@kevbonham](#):
>
> but `instantiate` can be a pretty involved computational action, and interrupting in the middle is not a great idea.

Yes, it isn’t obvious (to me at least) when the automatic `instantiate` should happen. When you start Julia? But maybe you just wanted to do `1+1`. When you go into the Pkg REPL? Maybe you just wanted to do a quick `status`, or load a package that is downloaded. Etc.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 7, 2020, 5:53pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/11 "2020-07-07T17:53:10Z")

</div>

could it be an additional command line startup flag like `--project` is? I don’t think doing it automatically is probably very safe, but providing some way to make it easier (as in fewer # steps) could be attractive.

---

<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:** [July 7, 2020, 7:28pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/12 "2020-07-07T19:28:37Z")

</div>

Ah there seems to be a misunderstanding. The scenario I mean seems rather straightforward to me. Namely when I do

```julia
Pkg.activate(xy)
using Mymodule

```

The julia may (Iirc) throw an error and complain that I need to run instantiate. In this instance I do not understand why it is not invoked automatically.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 7, 2020, 7:34pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/13 "2020-07-07T19:34:47Z")

</div>

Yes, that could be done. I had a proof of concept of something similar to that:

[https://github.com/JuliaLang/julia/pull/26469](https://github.com/JuliaLang/julia/pull/26469)

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 7, 2020, 8:21pm UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/14 "2020-07-07T20:21:53Z")

</div>

Ahh - this might be nice. Especially when you send something to a julia novice - explaining `instantiate` is just one more thing. If they could just `] activate` and have instantiation happen, I think that would be useful (and reasonable to have on by default).

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 8, 2020, 5:45am UTC](https://discourse.julialang.org/t/detect-if-current-package-has-been-activated/26764/15 "2020-07-08T05:45:01Z")

</div>

I actually asked something very similar on Slack recently because I got into the habit of putting `cd(@ __DIR__ ()); using Pkg; Pkg.activate("."); Pkg.instantiate()` at the top of script-type pieces of analysis that need to be reproducible on other people’s (who often aren’t Julia users) machines. It’s all now gone down the Slack memory hole, but the consensus then seemed to be that if people re-run the script, `instantiate` is a no-op, so there’s no harm in having it there (@kristoffer.carlsson: correct me if I’m wrong).
