Local/new package requires project to be activated

Hi,

I think I’m doing something wrong when creating packages. When I try to load a package I have created its dependencies are not found unless I also activate the project.

Example:

(v1.1) pkg> generate Test
Generating project Test:
    Test\Project.toml
    Test/src/Test.jl

(v1.1) pkg> dev Test
[ Info: resolving package specifier `Test` as a directory at `C:\temp\Test`.
 Resolving package versions...
  Updating `C:\....\.julia\environments\v1.1\Project.toml`
  [78f54ab0] + Test v0.1.0 [`..\..\..\..\..\temp\Test`]
  Updating `C:\....\.julia\environments\v1.1\Manifest.toml`
  [78f54ab0] + Test v0.1.0 [`..\..\..\..\..\temp\Test`]

(v1.1) pkg> activate Test 

(Test) pkg> add Distributions ## Note: I have this package installed in my global environment, this is just to add it as a dependency to Test
  Updating registry at `C:\.....\.julia\registries\General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `C:\temp\Test\Project.toml`
  [31c24e10] + Distributions v0.20.0
  Updating `C:\temp\Test\Manifest.toml`
  [List of dependencies omitted for brevity]

### Add "using Distributions" to Test.jl  ####

julia> using Test #Works!
[ Info: Precompiling Test [78f54ab0-7c75-11e9-3ad5-f9f36eb15d1f]

julia> Test.greet()
Hello World!
julia>  # Exit and try to load the module without activating project
Julia has exited. Press Enter to start a new session.

Starting Julia...
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0 (2019-01-21)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using Test #Fails! 
[ Info: Recompiling stale cache file C:\....\.julia\compiled\v1.1\Test\Vq5Bl.ji for Test [78f54ab0-7c75-11e9-3ad5-f9f36eb15d1f]
ERROR: LoadError: ArgumentError: Package Test does not have Distributions in its dependencies:
[stacktrace omitted for brevity]

(v1.1) pkg> activate Test

julia> using Test #Now it works!
[ Info: Precompiling Test [78f54ab0-7c75-11e9-3ad5-f9f36eb15d1f]

julia> Test.greet()
Hello World!

I have tried things like instantiate and adding the project to my “global” environment, but to no avail.

You are not doing anything wrong here: it is normal that you have to activate the environment of your package in order to be able to use its dependencies.

When you don’t activate anything, you are by default in an environment named after your Julia version (e.g. “v1.1”), which means that you have access to all packages that were added as dependencies of this default environment (which seems to not be the case of your locally developed project).

All the details about environments are given in the code loading chapter of the documentation.

2 Likes

Thanks for the help,

Indeed I did not have Distributions in my default environment. Adding it there got rid of the error (and doing Pkg.resolve() removed a warning about not having it in dependencies).

Also thanks for the link about code loading. It will come in handy when debugging dependency issues in the future, especially the methods to show dependency graphs for the project.

One follow up question then: After I have done “dev Test” I can’t seem to free the project.

(v1.1) pkg> free Test
ERROR: cannot free an unpinned package that does not exist in a registry

If I run “remove Test” it is removed, but then I can’t use it any more and running “add” gives an error message.

I am not sure it has anything to do with your situation, but I wonder if there is a possible interaction with the registered (standard) package Test?

1 Like

Thanks, I realized that too when writing my second response, but the issue comes from a real project which is not named Test.

Repeating the steps above with a project called testproj gives the same results.

Anyways, Freeing the project is something I think might resolve another issue I have involving performance and @everywhere and it is probably better to open a new thread about that issue once I can reproduce it in an explainable way.

For this issue I’ll just assume that any projects which are being developed needs dependencies imported explicitly and mark the reply from @ffevotte as the solution for this question.