# 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:** 50

<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:** [July 8, 2022, 5:39pm UTC](https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818/50 "2022-07-08T17:39:44Z")

</div>

Welcome! I am also still lost on environments, especially on how to get dependencies, versioning, and namespaces to work consistently among the suggested global, package, test, and example environments. It is especially confusing because the [Pkg documentation](https://pkgdocs.julialang.org/v1/environments/) is barren of any practical advice, and [PkgTemplates does not recommend](https://github.com/invenia/PkgTemplates.jl/issues/345) creating Project.toml files the same way that [Pkg does](https://pkgdocs.julialang.org/v1/creating-packages/#Test-specific-dependencies-in-Julia-1.2-and-above).

I have not been able to find a comprehensive guide, so I am still guessing by trial and error. Others can feel free to chime in and correct me. From what I can tell there are two main ways to run package code in the REPL:

## Method 1: using MyPackage

For this method, I activate a test environment that has `dev MyPackage`. That allows VSCode to track changes I make to the package files. I create a file to test from that I name `manual_testing.jl`, and I put the file inside the `MyPackage/test` folder (but it could go anywhere). The first line of the file is `using MyPackage`. I **do not** `using` any dependencies of `MyPackage`. The remaining lines of the file are of the form `x = 3` or `y = f(x)`.

**Pros**

1. I am testing `MyPackage` exactly how it would be run by others.
2. Changes to `MyPackage` are implemented instantly and automatically.
3. I can “Run and Debug” the entire `manual_testing.jl` file easily to find errors inside package functions.
4. Code can be sent from the `manual_testing.jl` file with Ctrl+Enter or typed directly in the REPL interchangeably and reliably.

**Cons**

1. I can only test full functions that are `export`-ed from `MyPackage`. I cannot test individual lines or segments of package code on their own unless I first wrap the lines in an exported function.
2. Only function inputs and outputs are available for inspection in the VSCode Workspace.
3. Pro #3 is really slow.

## Method 2: using All, Package, Dependencies

For this method, I activate the `MyPackage` environment and I send code directly from `MyPackage` `src` files to the REPL. The first line I send to the REPL is `using All, Package, Dependencies`. I **do not** `using MyPackage`. Then I navigate to the source file I want to edit and start sending lines from that file to the REPL (in global scope). There is a VSCode command called `Julia: Send Current Line or Selection to REPL` that I set a keyboard shortcut for to make this easier.

**Pros**

1. I can test line-by-line!
2. Iteration on ideas is way faster.
3. All variables are available for inspection in the VSCode Workspace.

**Cons**

1. VSCode can get confused about which module I want to work in (`MyPackage` or `Main`) and error out code that should work. I typically have to choose to type all code in the REPL **or** send all code to the REPL from file. If I chose type in REPL, then I can copy and paste from file. If I chose send from file, then I can type a line in the file and send it.
2. This will create a lot of temporary setup/test lines that will need to come back out for the final package.
3. I must redefine (resend to the REPL) every time I make changes to a function. This is as easy as Ctrl+Enter, but I often forget to redefine `f` before executing `f(x)` again (since this isn’t required with the other method).

I find that I need to switch back and forth between these methods depending on if I need granularity or rigor. Method 2 Con 1 is especially annoying though; @davidanthoff any recommendations for that?

---

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