How to reload a newly created package

I created a new package following the documentation. Specifically, I am stuck on this section where it says “and reloading the package…”. I cannot reload the package after restarting the REPL. If I did not screw something up, there probably is something missing in the documentation.

First I generate my package:

(v1.0) pkg> generate Thyrosim
Generating project Thyrosim:
    Thyrosim/Project.toml
    Thyrosim/src/Thyrosim.jl

Change directory to the package:

shell> cd Thyrosim/
/Users/biona001/.julia/dev/Thyrosim

Activate the package and import:

(v1.0) pkg> activate .
julia> import Thyrosim
[ Info: Precompiling Thyrosim [7ef34fca-2b35-11e9-1aeb-a527bedb189e]

julia> Thyrosim.greet()
Hello World!

Exit Julia and try to use it again:

julia> exit()
$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.3 (2018-12-18)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

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

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:823

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

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:823

How do I begin using my newly generated package?

You have to re-run activate Thyrosim when you restart the REPL.

I recommend reading the new Pkg quickstart: 2. Getting Started · Pkg.jl. It might make the “environments” workflow more clear.

1 Like

The package is only available in the activated project’s environment.

You’ll have to activate the same environment each time you restart Julia, to access added packages.

You could also add your local package to the default environment (v1.0).

This is good advice if you need a package available by default. But keep in mind that add will track a project through git. This adds a layer of indirection that can be confusing.

A simpler alternative is to use develop instead of add. develop will load a project’s source code directly as it exists on the filesystem.

1 Like

You can also launch julia with the --project flag, which is equivalent to doing pkg> activate .

1 Like

I put the following in .julia/config/startup.jl:

using Pkg
Pkg.activate("path/to/my/package")

This is convenient for me on Windows because I don’t start Julia from the command line. I double click the icon :sweat_smile: