Pattern for activating the current project

Just wanted to make sure that I’m using the new Pkg API as intended:

1 - every time I develop a standalone app I create a folder. Then I start a REPL, go into pkg> mode and activate . Then I add the dependencies to the project.

2 - when I need to run the app through the julia binary, the environment defined in the folder is not automatically loaded. This leads to errors if the dependencies of the project aren’t added to the global environment as well (which they shouldn’t be, that’s why we have project dependencies).

3 - thus, my entry file into the app starts with:

using Pkg
pkg"activate ."

Is this the right way to do it or can it be done better?

Also, why not have Julia automatically look for a Project.toml file in the current dir and if it exists, use that automatically? It seems to me that if somebody went through the trouble of adding the Project file, one wants to use it. It seems more probable to me that developers will forget to activate the environment, versus accidentally activating an environment due to a Project file that should not be there (it doesn’t even make sense to me to think that a Project file is there but should not be used).

3 Likes

You can choose project at startup

julia --project=path/to/project

or

julia --project

which will look for Project.toml in current directory and its parents. You can also use the JULIA_PROJECT environment variable to specify this.

See Should the CurrentEnv path be set at julia startup and not follow with the pwd? · Issue #27411 · JuliaLang/julia · GitHub, Automatically load environments? · Issue #360 · JuliaLang/Pkg.jl · GitHub

4 Likes

Thanks, that’s good to know.

Am I abusing the API if I use this in the main file (I would say “no”):

using Pkg
pkg"activate ."

direnv sounds like a good way to handle it, thanks.