When should I `activate`?

I understand this is a super basic question, but I don’t think I know the correct words to find similar questions that have already answered this.

I’m attempting to make a package, using Revise.jl. I’m following the cookbook. While I am able to get it to work, I’m curious as to why the cookbook doesn’t have to activate the package before calling using MyPkg while it seems like it doesn’t work for me until I run pkg> activate . at the top level of the package. If I don’t, I get an error:

ERROR: ArgumentError: Package MyPkg not found in current path.
- Run `import Pkg; Pkg.add("MyPkg") to install the MyPkg package.

Can someone explain to me why I have to activate while the cookbook does not?

Thank you!

What version of Julia are you using? In newer versions using MyPkg will prompt you to install it (thus, I think the omission in those docs). In your case the package would get installed in your main environment, but that is not really recommendable.

If you are developing MyPkg, always be sure to be in the environment MyPkg>, either by using activate . in the folder, or starting julia with julia --project. You should do that.

(to using MyPkg work directly, the package must be installed in the active environment)

2 Likes

I meant to generalize my example from GPlateMyPkg but forgot to do it in most places - sorry if this changes the answer. Editing my post.

It looks to me like that tutorial is skipping some steps regarding the environment. Any package you use will ultimately need to be installed (with add or dev) somewhere in your environment stack.

TLDR

To use MyPkg, either activate the MyPkg environment with pkg> activate path/to/MyPkg or activate a different environment that has MyPkg installed in it. You can check which packages are installed in your current environment with pkg> status.

Option 1:

Create a new shared environment. Add MyPkg and Revise to it.

(@v1.10) pkg> activate @TestMyPkg
  Activating new project at `C:\Users\nboyer.AIP\.julia\environments\TestMyPkg`

(@TestMyPkg) pkg> add Revise
    Updating registry at `C:\Users\nboyer.AIP\.julia\registries\General.toml`

(@TestMyPkg) pkg> dev C:\Users\nboyer.AIP\.julia\dev\MyPackage
   Resolving package versions...

(@TestMyPkg) pkg> status
Status `C:\Users\nboyer.AIP\.julia\environments\TestMyPkg\Project.toml`
  [4b534a9b] MyPackage v0.1.0 `C:\Users\nboyer.AIP\.julia\dev\MyPackage`
  [295af30f] Revise v3.5.7

julia> using Revise, MyPackage
Precompiling MyPackage
  1 dependency successfully precompiled in 2 seconds

julia> # edit MyPackage and test code here

Option 2

Many people choose to install Revise and other developer tooling packages into their default environment (e.g. @v1.10). Any package installed in your default environment is also available within any other environment. With the default environment set up this way, you can get basically the same functionality as above just by activating your package’s local environment.

(@v1.10) pkg> add Revise
   Resolving package versions...
  No Changes to `C:\Users\nboyer.AIP\.julia\environments\v1.10\Project.toml`
  No Changes to `C:\Users\nboyer.AIP\.julia\environments\v1.10\Manifest.toml`

(@v1.10) pkg> activate C:\Users\nboyer.AIP\.julia\dev\MyPackage
  Activating project at `C:\Users\nboyer.AIP\.julia\dev\MyPackage`

(MyPackage) pkg> status
Project MyPackage v0.1.0
Status `C:\Users\nboyer.AIP\.julia\dev\MyPackage\Project.toml`
  [37e2e46d] LinearAlgebra

julia> using Revise, MyPackage

julia>  # edit MyPackage and test code here
5 Likes

To clarify, this is true in interactive development (which is generally the only time you’d want Revise loaded, so it makes sense I’m this context). If you need something within your package code, it needs to be added in the package environment.