Understanding environments and DrWatson

Suppose I have a package that looks like this

module MyPkg
   using Distributions, DataFrames
   function a() end

There is an associated environment with this that has Project.toml.Manifest.toml files that tell MyPkg exactly what version of Distributions and DataFrames to use.

Now suppose I created a DrWatson.jl project. By doing this, DrWatson has set up a project directory for me with relevant folders and also created a Project.toml/Manifest.toml files, and activated it. For clarity, let’s say this project is called contact_tracing and my prompt now looks like this: (contact_tracing) pkg>.

Now they suggest to add my my package, so basically (contact_tracing) pkg> add MyPkg and then in my script file I have using MyPkg.

So my question is, when I type in using MyPkg, what versions of Distributions and DataFrames is going to get pulled? The latest ones tagged ones? Because that’s not what I want. I’ve already specified in the package Project.toml which particular version I want, so it should be able to get that right?

Don’t edit the Project.toml directly.

Use add DataFrames@version_numer to add a particular package version you want.

1 Like

MyPkg should give bounds on the versions of its dependencies. Now when another package installs it, it will try to install the latest possible version of the dependncies. To choose a particular one, do as @pdeffebach wrote. You can then also pin a package, which means that if you later do an update, it will not update.

This dosn’t really work. Let’s go through an entire example. I have a brand new package called mypkg. I add an older dependency of DataFrames to this (current version is 0.21)

(mypkg) pkg> add DataFrames#v0.20.2
  Resolving package versions...
   Updating `~/.julia/dev/mypkg/Project.toml`
  [a93c6f00] + DataFrames v0.20.2 #v0.20.2 (https://github.com/JuliaData/DataFrames.jl.git)

I know that now when I type in using mypkg it will use v0.20.2 (as long as the environment is activated).

Now I create a DrWatson project. Lets called it my_science_project. The science project uses the package mypkg defined above, and so we will add that in a few steps. First, lets initialize (from global environment)

julia> using DrWatson
julia> initialize_project(".")
 Activating new environment at `~/my_sci_project/Project.toml`
   Updating registry at `~/.julia/registries/General`
   Updating git-repo `https://github.com/JuliaRegistries/General.git`
  Resolving package versions...
   Updating `~/my_sci_project/Project.toml`
  [634d3b9d] + DrWatson v1.13.1

Great. Now let’s activate this project and verify the environment has changed.

julia> quickactivate(".") ## could also use Pkg.activate
 Activating environment at `~/my_sci_project/Project.toml`

(my_sci_project) pkg>

I am going to add my dependency now (note: I have to use dev here since the folder exists in my julia dev path)

(my_sci_project) pkg> dev mypkg
Path `/home/affans/.julia/dev/mypkg` exists and looks like the correct package. Using existing path.
  Resolving package versions...
   Updating `~/my_sci_project/Project.toml`
  [08773ad6] + mypkg v0.1.0 [`~/.julia/dev/mypkg`]
   Updating `~/my_sci_project/Manifest.toml`
  [324d7699] + CategoricalArrays v0.8.1
  [34da2185] + Compat v3.12.0
  [9a962f9c] + DataAPI v1.3.0
  [a93c6f00] + DataFrames v0.21.2

We see that it added version 0.21.2 (the latest version) for DataFrames in the Manifest? This is not what I want. The functions inside will break if using v0.21.2 and only work with v0.20.2. How can I force this dependency?

I don’t expect my end users to manually look at the Project file of my package and then add that in manually in their drwatson projects.

I think this is solved if you pin DataFrames in mypkg, not just add a version, sorry.

btw, what functions are broken on DataFrames? Perhaps we can help you update those parts of your code.

Oh I see. I will try pinning the package. I thin alternatively, I have to use [compat] from reading the documentation.

There is nothing special about DataFrames and I was just using it as an example to explain what I was trying to do.

Edit: Yes, so adding compat worked. I havn’t tried pinning but that may also work as well.