Well I got three entirely opposed answers again. Is there not a built-in way that just works? How are new users supposed to take the advice to put all their code in functions and packages if everything breaks when they do? Sorry if I sound ungrateful, but I am really frustrated with this process. Given that reproducible environments and interactive development are two of the most touted features of Julia, I am surprised by how difficult it is to get them to work. Let me ask more direct questions with full examples.
MyPackage.jl
module MyPackage
using LinearAlgebra: norm
include("myfile.jl")
end
myfile.jl
export f
function f(x)
function f_sub(y)
norm(y)
end
2 * f_sub(x)
end
Package Dependencies
That doesn’t sound safe. I can use packages in the REPL that aren’t in my Package/Environment? This is why I am nervous that I am testing/exploring things that will not work in production.
julia> using MyPackage
(MyPackage) pkg> status
Project MyPackage v0.1.0
Status `C:\Users\nboyer.AIP\.julia\dev\MyPackage\Project.toml`
[37e2e46d] LinearAlgebra
julia> using DataFrames
(MyPackage) pkg> status
Project MyPackage v0.1.0
Status `C:\Users\nboyer.AIP\.julia\dev\MyPackage\Project.toml`
[37e2e46d] LinearAlgebra
julia> df = DataFrame(x=[3,4]) # Typed
2×1 DataFrame
Row │ x
│ Int64
─────┼───────
1 │ 3
2 │ 4
julia> df = DataFrame(x=[3,4]) # Ctrl + Enter
ERROR: UndefVarError: DataFrame not defined
The last line errors if I run it from file, which is the behavior I would want, but once again the REPL behaves differently if I type code vs if I Ctrl+Enter it. Seems like I should avoid typing anything except using MyPackage
in the REPL.
Ensuring that I only using MyPackage
fixes the ambiguity above, but then it makes interactive development impossible.
Interactive Development
julia> using MyPackage
julia> y = [3, 4]
2-element Vector{Int64}:
3
4
julia> norm(y) # Ctrl+Enter
ERROR: UndefVarError: norm not defined
Stacktrace:
[1] top-level scope
@ REPL[3]:1
julia> norm(y) # Typed
ERROR: UndefVarError: norm not defined
Stacktrace:
[1] top-level scope
@ REPL[4]:1
julia> using LinearAlgebra: norm
julia> norm(y)
5.0
Because of the previous point, I think the LinearAlgebra
version used here could be different from the version locked in MyPackage
. I don’t know how to reconcile these first two categories. I need safety and usability.
(I have also had the opposite behavior occur where I get an error from y
not being defined rather than from norm
, but I can’t seem to reproduce that right now.)
Sub-Functions
julia> using MyPackage
julia> f([3,4])
10.0
julia> MyPackage.f_sub([3,4])
ERROR: UndefVarError: f_sub not defined
I could of course move my sub-function outside and before the main function, but that seems weird if the sub-function only makes sense in the context of the main function. Is there a way to test these interactively or should I really eliminate all function nesting? I could use the debugger every time I need to test sub-functions, but that is slow and comes with its own set of problems.
Debugging
I figured out while writing this question that I should be debugging from a file in the test
directory rather than from the src
files, which explains why my debugger wasn’t doing anything. I am still confused about how to keep the test dependencies loaded and in sync with the package dependencies though.
Testing
I don’t follow what you mean by global environment. Do you mean MyPackage
, test
, or 1.7
?
Is TestEnv.activate()
different than `Pkg.activate(“test”)?
Is the typical workflow to tinker and debug from a test/manual_testing.jl
file and then to formalize that code into test/runtests.jl
once it is working then?