Question regarding Pkg environments

Hello.
I am confused about using Pkg environments.

Suppose I create a new directory on my system called “NewProject”.
Then I change my directory to this new folder, launch Julia, hit ] to launch Pkg and type activate ..
Now, Pkg tells me that the active environment is “NewProject” by displaying: (NewProject) pkg>.

With my new environment active, if I then run using X for some package X that is installed in the “global” default environment (v1.3), Julia loads the package just fine, even though I have not added it to the active environment (NewProject) with add X in Pkg. What I would like is for this new environment to be completely separate from the global environment, and for Julia to complain that the package that I am trying to load does not exist (in the active environment NewProject). Is this the way Pkg is supposed to work, or am I doing something wrong? Can I somehow configure Pkg to only see the packages that have been added to the active environment?

You may be looking for something like

empty!(LOAD_PATH)
push!(LOAD_PATH, "@")

See https://docs.julialang.org/en/latest/manual/code-loading/#Environment-stacks-1, and ?LOAD_PATH.

1 Like

Thank you very much.
This is exactly what I meant. Too bad there is no indication of this in the Pkg documentation.

Note also that Pkg.test will complain if dependencies are missing from Project.toml because tests run in a “clean” environment.

I think there is, 8. Glossary · Pkg.jl has

Environment: the combination of the top-level name map provided by a project file combined with the dependency graph and map from packages to their entry points provided by a manifest file. For more detail see the manual section on code loading.

Yes, that defines what an environment means, but does not explain how they work in practice.

In my opinion, something about this could be written here in the section that is labeled “Working with Environments”:
https://julialang.github.io/Pkg.jl/v1/environments/

Currently, there is a line that says “This new environment is completely separate from the one we used earlier.” after showing how an environment can be activated. Without the modifications to LOAD_PATH you suggested, I find this to be false.

I am not sure if you missed this part:

For more detail see the manual section on code loading.

which is exactly the section I linked above. So there is some indication of the relevant docs in the Pkg docs. Just not the details. But I am not sure they belong there.

Perhaps you could make a PR to the tutorial part of the Pkg docs, but keep in mind that the use case in this topic is not the common one.