# Default project activation via Pkg.jl or DrWatson.jl

**URL:** <https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157>\
**Category:** General Usage\
**Tags:** pkg, drwatson\
**Created:** [February 6, 2023, 6:33pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157 "2023-02-06T18:33:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [February 6, 2023, 6:33pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/1 "2023-02-06T18:33:22Z")

</div>

Right now my, I have several directories, which have Project.toml files because of 2 situations:  
(1) I have cloned them from Github repos, or  
(2) I have created them myself as DrWatson.jl “scientific” projects.

In order to have the corresponding environments automatically loaded when I launch Julia, I created a startup.jl file reading:

```julia
using OhMyREPL
using DrWatson
if isfile("Project.toml") && isfile("Manifest.toml")
    quickactivate(".")
end

```

It works ok for the case (2) above, but I would like to contemplate case (1) as well, where the command quickactivate is not relevant (despite not issuing any errors…); in this case (1) I guess I would like to run something like

```julia
using Pkg
activate(".")

```

Which conditional should I test to contemplate this case (1)? It seems the Project.toml file does not have a variable `name` set instead of what happens in case (2) with the DrWatson projects…

How should I check this and change my `startup.jl` file accordingly? I hope I have been clear, but am not sure…  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [February 6, 2023, 6:58pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/2 "2023-02-06T18:58:22Z")

</div>

Is this a good idea? it seems to me that it can lead to a lot of accidentally created environments. Anyway,

I would think of using DrWatson’s findproject. Something like:

```julia
using DrWatson
import Pkg
project_found = findproject(".") #Will look recursively for an existing project, similar to quickactivate
project_dir = isnothing(project_found) ? "." : project_found
Pkg.activate(project_dir)

```

Another option is to run julia as `julia --project=@.`, which you can do with an alias.

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [February 6, 2023, 7:07pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/3 "2023-02-06T19:07:51Z")

</div>

`DrWatson.quickactivate` or `DrWatson.@quickactivate` should work irrespectively whether it’s a github repo or a DrWatson project or a local package. It will always activate the environment as long as a parent directory contains at least a `Project.toml`.  
Maybe you are not in the correct directory `"."` that you need to activate ?

I would totally avoid having something like this in my `startup.jl`:

```julia
using Pkg
activate(".")

```

because imagine starting up julia from `$HOME/../myproject/src/` you will end up activating `src`, which is not desirable.

As a reminder, running `julia --project` most of the times is what you want as it will also search the parent directories and activate the first `Project.toml` found.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [February 6, 2023, 7:34pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/4 "2023-02-06T19:34:02Z")

</div>

@filchristou

I sure was in the directory with the .toml file and my current startup.jl file did correctly activate its project, via DrWatson’ s `quickactivate` command there, but my problem is that, in case (1) I really do not have a bona fide DrWatson “scientific” project and some commands, such as `projectname()` does not work as expected, because it returns `Nothing` instead of the name of the directory…

As concerns the command block:

```julia
using Pkg
activate(".")

```

I would use it, if at all, only inside an if or elseif block, with the conditional to check for the existence of the .toml file…

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [February 6, 2023, 7:44pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/5 "2023-02-06T19:44:18Z")

</div>

@aramirezreyes

I was not thinking of using the commands

```julia
using Pkg
activate(".")

```

outside of a conditional if block. Then I guess it would not “lead to a lot of accidentally created environments”, right?

Your piece of code seems to be a good suggestion for both cases (1) and (2). I wonder however what it would miss by not using DrWatson’s `quickactivate` command; for instance, the call `projectname()` returns `Nothing` instead of the expected (?) directory name containing the .toml file. The `findproject()` on its turn does output the full path of this directory… After all, a DrWatson project is somewhat different from a pure Pkg project, right?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [February 6, 2023, 7:59pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/6 "2023-02-06T19:59:50Z")

</div>

I don’t think you will lose anything. The [code for quickactivate](https://github.com/JuliaDynamics/DrWatson.jl/blob/c6c55dfbc43ddc97c8ccfb540e9e79f76f6682ba/src/project_setup.jl#L131) does not do anything fancy at all.

```julia
function quickactivate(path, name = nothing)
    projectpath = findproject(path)
    if projectpath === nothing || projectpath == dirname(Base.active_project())
        return nothing
    end
    Pkg.activate(projectpath)
    if !(name === nothing) && projectname() != name
        error(
        "The activated project did not match asserted name. Current project "*
        "name is $(projectname()) while the asserted name is $name."
        )
    end
    return nothing
end

```

In the end it uses `Pkg.activate` if it finds a project. The only difference with what you want is that it will activate a project in the current working directory. In this sense I don’t think a DrWatons `Project` is any different than a regular one.

I think I agree with @filchristou that what you want is just a use quickactivate, quickactivate will work for any project, regardless if it was created using DrWatson or not.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 6, 2023, 8:24pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/7 "2023-02-06T20:24:51Z")

</div>

> [@aramirezreyes](#):
>
> quickactivate will work for any project, regardless if it was created using DrWatson or not.

correct!

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [February 6, 2023, 8:40pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/8 "2023-02-06T20:40:41Z")

</div>

So to sum up, you are suggesting/recommending that my `startup.jl` might stay as is:

```julia
using OhMyREPL
using DrWatson
if isfile("Project.toml") && isfile("Manifest.toml")
    quickactivate(".")
end

```

so that, there would be a default project activation if and only if I launch julia from a directory where there are both `Project.toml` and `Manifest.toml` files, no matter how they were created. Is that it?  
Thanks again!

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 6, 2023, 8:43pm UTC](https://discourse.julialang.org/t/default-project-activation-via-pkg-jl-or-drwatson-jl/94157/9 "2023-02-06T20:43:02Z")

</div>

You do not need `DrWatson` for this block… Since you are anyways explicitly checking for the existence of a project file you can replace `quickactivate(".")` with just `Pkg.activate(".")`.

Edit: I am assuming you DO NOT want the extra functionality `quickactivate` has of searching on parent folders of current folder.
