# Executing code blocks with REPL workspace

**URL:** <https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572>\
**Category:** General Usage\
**Created:** [December 13, 2023, 3:51pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572 "2023-12-13T15:51:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kealme](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@kealme](https://discourse.julialang.org/u/kealme)\
**Post date:** [December 13, 2023, 3:51pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/1 "2023-12-13T15:51:31Z")

</div>

Hi,

I have a very basic question about Julia: I’ve written a function in julia and im trying to debug the code. I can mark a block of code and execute it using Shift+Enter.

However, when executing the code like this, the workspace seems not to be known for Julia.

Example:  
My function is `load_data(filepath)`. I wanna just run this line of code. I defined the `filepath` in the REPL to make it work, but it just throws an UndefVarError.

What basic concept am I not getting here?

Best

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 13, 2023, 5:02pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/2 "2023-12-13T17:02:38Z")

</div>

I’m not sure if I fully understand your situation. Are you using VS Code and sending code to the REPL?

The REPL does not know what is in your file. If you did not send a line such as the following then the variable will not be defined.

```julia
filepath = "my_secret_file.txt"

```

There is also global and local scope to consider.

If you type `filepath` in the REPL, is it defined?

Could you show us the block of code thay produces the error?

---

<div class="post-metadata">

**Author:** ![kealme](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@kealme](https://discourse.julialang.org/u/kealme)\
**Post date:** [December 13, 2023, 5:17pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/3 "2023-12-13T17:17:10Z")

</div>

Hi,  
thanks for your reply. It’s as follows:  
If I mark the following lines of code in my `code.jl` file and execute it, everything works fine:

```julia
filepath = "my_secret_file.txt"
load_data(filepath)

```

However, when I define filepath using the REPL (`filepath = "my_secret_file.txt"`) and then just mark the second line and execute only this from the file

```julia
load_data(filepath)

```

I obtain the error, that the variable `filepath` is not defined (`UndefVarError`)

Best

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [December 13, 2023, 5:40pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/4 "2023-12-13T17:40:03Z")

</div>

Welcome to the Julia discourse.

From afar still a bit hard to tell without more details. If the script itself running (from which folder?) would find the file, then the Julia REPL might be started in a different folder? You could `cd` into the right folder as one command in REPL then after the start?  
With `;` you also have a shell mode in Julia REPL (a red `shell>` upfront) where you can easier check with cd and ls and such where you are and where to go.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 13, 2023, 5:41pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/5 "2023-12-13T17:41:39Z")

</div>

Please confirm. Are you are using VS Code or another IDE or editor?

---

<div class="post-metadata">

**Author:** ![kealme](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@kealme](https://discourse.julialang.org/u/kealme)\
**Post date:** [December 14, 2023, 7:32am UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/6 "2023-12-14T07:32:46Z")

</div>

Hi, thanks for your reply. Yes, I’m using the latest release of VS Code.

I just played around a litttle bit and observed the following:  
The problem above does not appear (aka everything works) when I’m using a fresh Julia session.  
Since the function `load_data()` is part of my package, I then import the package with `using MyPackage` in the REPL, which makes the function `load_data()` available. But than, everytime I execute the code snippet, it does not know variables defined through the REPL.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 14, 2023, 10:23am UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/7 "2023-12-14T10:23:49Z")

</div>

I’m having to extrapolate here since you are not providing any example code. What I understand is that you have a package as follows.

```julia
module MyPackage

export load_data

function load_data(filename)
    println("Loading ", filename)
end

end

```

You use it as follows in the REPL.

```julia-repl
julia> using MyPackage

julia> filename = "my_secret_file.txt"
"my_secret_file.txt"

julia> load_data(filename)
Loading my_secret_file.txt

julia> filename2 = "my_secret_file2.txt"
"my_secret_file2.txt"

julia> load_data(filename2)
Loading my_secret_file2.txt

julia> filename3 = "my_other_file.txt"

```

In a file, in VS Code you have the following.

```julia
load_data(filename3)

```

You push Ctrl-enter in vscode, and you get

```julia
julia> load_data(filename3)
ERROR: UndefVarError: `filename3` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[30]:1

```

Is that right? How did you start the REPL?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 14, 2023, 10:37am UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/8 "2023-12-14T10:37:20Z")

</div>

To check, I want to make sure that you are starting the Julia REPL via the VS Code Julia Extension by either pressing “Alt-J, Alt-O” or doing “Ctrl-Shift-P” and selecting “Julia: Start REPL”. That should start up a REPL and be the REPL that VS Code will sends the code to execute on.

What I’m worried about is that you are starting the Julia REPL by creating a new terminal, and then running the Julia REPL via the command line as follows. If you do this, then you have two Julia sessions that do not know about each other.

---

<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:** [December 14, 2023, 1:19pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/9 "2023-12-14T13:19:58Z")

</div>

This happens all the time for me, and it is very annoying. Same issue as here:

> [@VSCode REPL Namespaces/Modules](https://discourse.julialang.org/t/vscode-repl-namespaces-modules/103170):
>
> How is this still happening? How are you living with this? I expected the [REPL module switching](https://github.com/JuliaLang/julia/pull/33872) released with Julia 1.9 to fix this issue. (DataFrames) julia\> x = 3 # Ctrl + Enter 3 (DataFrames) julia\> x # Typed into REPL ERROR: UndefVarError: `x` not defined I have a clone of the DataFrames package open. [image] I have activated the current directory environment. [image] [image] I have chosen the DataFrames module as the active module with the VSCode GUI. [image] I have used the n…

You need to change the REPL module to match your package module. Otherwise, the REPL and package namespaces are kept separate. I still do not understand why this isn’t done automatically.

---

<div class="post-metadata">

**Author:** ![kealme](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@kealme](https://discourse.julialang.org/u/kealme)\
**Post date:** [December 15, 2023, 7:39am UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/10 "2023-12-15T07:39:46Z")

</div>

Thx @mkitti for your very detailed explanation the different terminals.

Thank you @Nathan_Boyer Nathan, that’s the issue I had!

Now it works almost… If I import my package I have to switch the modules from the REPL and from VSCode to the same package. But the VS code workspace explorer only shows the workspace of the base-module (so I can’t see any variables defined in the module-workspace itself). How can I change that?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 15, 2023, 4:40pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/11 "2023-12-15T16:40:22Z")

</div>

Are you using Revise.jl?

> **[Revise usage: a cookbook · Revise.jl](https://timholy.github.io/Revise.jl/stable/cookbook/)**
>
> Documentation for Revise.jl.

That makes most of this unnecessary during package development.

---

<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:** [December 15, 2023, 5:09pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/12 "2023-12-15T17:09:13Z")

</div>

> [@kealme](#):
>
> I’m using the latest release of VS Code.

VSCode automatically uses Revise.jl.

> [@mkitti](#):
>
> That makes most of this unnecessary during package development.

I wish it was unnecessary.

I think most people get around this limitation by working in the Main REPL module and only typing/executing full function calls defined in their package (Method 1 in [this link](https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818/50)), but that method doesn’t work very well if you need to test individual lines of your package code (rather than entire functions). Changing your REPL module is the only way I have found to do that.

I’m happy to be proven wrong and shown something simpler, but I’ve been asking about this for a long time, and no one has given me a good answer:

> [@How to use VSCode and REPL to write and test a package?](https://discourse.julialang.org/t/how-to-use-vscode-and-repl-to-write-and-test-a-package/78818):
>
> I have gone through a multitude of tutorials now but still can’t seem to wrap my head around the basics of how to write and test a package as I go. I don’t understand the proper way to add my package and its dependencies to the REPL namespace or the proper way to juggle between the Main and MyPackage modules. I keep getting errors that variables, functions, or packages are undefined. I just want the simplest, foolproof workflow. Open MyPackage folder in VSCode made with PkgTemplates.jl. Start …

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 15, 2023, 6:48pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/13 "2023-12-15T18:48:11Z")

</div>

If it’s a few lines of code, then I just use do `import MyPackage: A, B, C`.

There is also an [export all macro call](https://discourse.julialang.org/t/exportall/4970/16) that can be used.

```julia
# export all
for n in names(@ __MODULE__ ; all=true)
    if Base.isidentifier(n) && n ∉ (Symbol(@ __MODULE__ ), :eval, :include)
        @eval export $n
    end
end

```

Usually though I might start a function within the package of interest and with just the lines of interest I want to test, and then run that from the REPL.

There are also packages such as [GitHub - JuliaDebug/Infiltrator.jl: No-overhead breakpoints in Julia](https://github.com/JuliaDebug/Infiltrator.jl) that you let step into a local environment of a function.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 15, 2023, 6:54pm UTC](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/14 "2023-12-15T18:54:14Z")

</div>

Some combination of the following is helpful to me

1. An exportall(mod::Module)` function

```julia
function exportall(mod)
    for n in names(mod, all = true)
        if Base.isidentifier(n) && n ∉ (Symbol(mod), :eval)
            @eval mod export $n
        end
    end
end

```

I have this in my startup.jl

1. A `main()` function inside the module which looks like

```julia
function main()
@eval Main begin
    ...
end
end

```

The only problem is that you need to make sure you run `exportall` before `main()`

This has a nice combination of

1. Revise.jl works
2. You can play around with functions inside your module
3. The temporary variables you make at the REPL aren’t visible inside your module, so you don’t accidentally define global things that get used in your module.
