Julia recompiling unchanged modules

Hi all,

I’m using julia 1.5.3 on Ubuntu.

I have a bunch of modules with dependencies, and a one line script that just has a using statement for these modules (no other code in the script).

I first precompile all my modules using this code in a loop:

using Pkg
Pkg.activate("<module name>")
Pkg.precompile()
Pkg.activate()

(I’ve also tried using Base.compilecache instead of Pkg.precompile with the same results.)

After the precompile, I try to load my one line script into julia:

julia init-pkgs.jl

Julia recompiles all the modules again. Subsequent runs of this code do not result in recompiles.

I don’t quite understand why this happens. Is Pkg.precompile / Base.compilecache insufficient for compiling the code? Is there anything else I can do ahead of time so that running my script does not cause a recompile?

Thanks,
Philip

Hi Philip,

You may be confused about what Pkg.activate does. It changes the environment.

You should just call Pkg.precompile() in the environment that you want to run the script. This may be your main environment or perhaps a specific one for your script.

My recommendation is to create a specific environment for your script so the results will be reproducible:

  1. In Julia, cd to the directory where your script is.
  2. Run Pkg.activate(".") or ]activate . to create a new environment in the current directory.
  3. Use Pkg.add or ]add to add your dependencies.
  4. Run Pkg.precompile() to precompile the packages in the current enviornment
  5. When running your script, use julia --project=. your_script_name.jl. This activates the environment in the current directory and then runs your script.

Thanks, I’ll try this.
I already have an environment for the script with all dependencies set up but I haven’t used --project so that may be the issue.

1 Like

Julia would just use the base environment for your Julia version if you do not specify that from the command line. Good luck.

What does @. mean and how is it different from .?

From Getting Started · The Julia Language

--project[={<dir>|@.}]

Set as the home project/environment. The default @. option will search through parent directories until a Project.toml or JuliaProject.toml file is found.

Also see the following note:

In Julia 1.0, the default --project=@. option did not search up from the root directory of a Git repository for the Project.toml file. From Julia 1.1 forward, it does.