# How to use VSCode and REPL to write and test a package?

**URL:** <https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818>\
**Category:** VS Code\
**Tags:** question, package\
**Created:** [March 31, 2022, 3:28pm UTC](https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818 "2022-03-31T15:28:31Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [March 31, 2022, 8:52pm UTC](https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818/6 "2022-03-31T20:52:53Z")

</div>

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**
>
> ```julia
> module MyPackage
> 
> using LinearAlgebra: norm
> 
> include("myfile.jl")
> 
> end
> 
> ```

> **myfile.jl**
>
> ```julia
> export f
> 
> function f(x)
> function f_sub(y)
> norm(y)
> end
> 2 * f_sub(x)
> end
> 
> ```

# Package Dependencies

> [@davidanthoff](#):
>
> That project is _always_ available, even if you activate another project because it is always in the `LOAD_PATH` .

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
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.

> [@lmiq](#):
>
> `using MyPackage` only

Ensuring that I only `using MyPackage` fixes the ambiguity above, but then it makes interactive development impossible.

# Interactive Development

```julia
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](https://github.com/julia-vscode/julia-vscode/issues/2816#issuecomment-1081928986) right now.)

# Sub-Functions

> [@lmiq](#):
>
> But if you want to test `f_sub` only, you need to use `MyPackage.f_sub`

```julia
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

> [@jlperla](#):
>
> Just to be clear: at no point do you need to add your package to theglobal environment or manually make any temp project file, and you definetely don’t want to put test-only dependencies in the global environment,

I don’t follow what you mean by global environment. Do you mean `MyPackage`, `test`, or `1.7`?

> [@jlperla](#):
>
> To work interactively, given that your package is activated just go `using TestEnv; TestEnv.activate()` when you start up the REPL

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?

---

_[View the full topic](https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818)._
