# @nbinclude in the REPL

**URL:** https://discourse.julialang.org/t/nbinclude-in-the-repl/30467
**Category:** New to Julia
**Created:** [October 29, 2019, 11:46pm UTC](https://discourse.julialang.org/t/nbinclude-in-the-repl/30467 "2019-10-29T23:46:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![PhilipDLoewen](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@PhilipDLoewen](https://discourse.julialang.org/u/PhilipDLoewen)
#### Post date: [October 29, 2019, 11:46pm UTC](https://discourse.julialang.org/t/nbinclude-in-the-repl/30467/1 "2019-10-29T23:46:00Z")

</div>

Maybe this is a bug report. Please advise.

I have a small notebook that runs perfectly when I open it in Jupyter, but not at the REPL.

Working at a freshly-started Julia 1.2.0 REPL, when I say

```julia
using NBInclude
@nbinclude("CALO-MWE.ipynb")

```

I expect the minimalist workbook attached here to run successfully and print the computed result, but instead I get the error message below:

```julia
ERROR: LoadError: UndefVarError: slacksum not defined
Stacktrace:
 [1] top-level scope at /home/loew/loewCloud/CALO/2019-10/CALO-MWE.ipynb:In[2]:4
 [2] include_string(::Module, ::String, ::String) at ./loading.jl:1064
 [3] my_include_string(::Module, ::String, ::String, ::Nothing, ::Bool) at /home/loew/.julia/packages/NBInclude/m4rfj/src/NBInclude.jl:29
 [4] #nbinclude#1(::Bool, ::UnitRange{Int64}, ::Regex, ::typeof(identity), ::Bool, ::typeof(nbinclude), ::Module, ::String) at /home/loew/.julia/packages/NBInclude/m4rfj/src/NBInclude.jl:82
 [5] nbinclude(::Module, ::String) at /home/loew/.julia/packages/NBInclude/m4rfj/src/NBInclude.jl:53
 [6] top-level scope at REPL[14]:1
in expression starting at /home/loew/loewCloud/CALO/2019-10/CALO-MWE.ipynb:In[2]:3 

```

I have Julia 1.2.0, with IJulia v1.20.0 and NBInclude v2.1.0.

What do you suggest?

Sorry, new here, don’t know how to attach the file named CALO-MWE.ipynb, so I’ll just paste it here:

```julia
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "slacks = [1,2];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Total slack: 3.\n"
     ]
    }
   ],
   "source": [
    "slacksum = 0;\n",
    "\n",
    "for sl in slacks\n",
    " slacksum += sl\n",
    "end\n",
    "println(\"\\nTotal slack: $(slacksum).\")"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 1.2.0",
   "language": "julia",
   "name": "julia-1.2"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [October 30, 2019, 7:45am UTC](https://discourse.julialang.org/t/nbinclude-in-the-repl/30467/2 "2019-10-30T07:45:40Z")

</div>

I think you might be running into a scope issue, although I’m not 100% sure how scoping works in notebooks run from the REPL.

My best guess: your code includes the loop

```julia
slacksum = 0
for sl in slacks
    slacksum += sl
end

```

This works in the notebook because Notebooks by default use `SoftGlobalScope.jl`, however if you run the same code in the REPL, which doesn’t use `SoftGlobalScope` it raises an error because the loop creates its own scope, in which the variable `slacksum` is not defined. The solution is to annotate the line in the loop with `global`, to indicate that the loop is accessing a variable that’s defined in global scope.

The general solution to these things is not to run code in global scope, but to enclose everything in a function, although I see how this can be tricky in Notebooks with cell by cell execution.

---

<div class="post-metadata">

### Author: ![pontus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pontus/32/10438_2.png) [@pontus](https://discourse.julialang.org/u/pontus)
#### Post date: [October 30, 2019, 1:38pm UTC](https://discourse.julialang.org/t/nbinclude-in-the-repl/30467/3 "2019-10-30T13:38:10Z")

</div>

There is a bit more info on the SoftGlobalScope here:  
[https://github.com/stevengj/NBInclude.jl/issues/11](https://github.com/stevengj/NBInclude.jl/issues/11)  
and here:  
[https://github.com/stevengj/SoftGlobalScope.jl](https://github.com/stevengj/SoftGlobalScope.jl)

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [October 30, 2019, 2:30pm UTC](https://discourse.julialang.org/t/nbinclude-in-the-repl/30467/4 "2019-10-30T14:30:12Z")

</div>

Yes. IJulia has a patch for the scoping issue. To apply for your example, go `@nbinclude(foo.ipynb, softscope=true)`

---

<div class="post-metadata">

### Author: ![PhilipDLoewen](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@PhilipDLoewen](https://discourse.julialang.org/u/PhilipDLoewen)
#### Post date: [October 30, 2019, 4:05pm UTC](https://discourse.julialang.org/t/nbinclude-in-the-repl/30467/5 "2019-10-30T16:05:08Z")

</div>

Thanks for the informative responses. The suggestion from @jlperla works for me, after changing the comma to a semicolon (and installing the SoftGlobalScope package):

```julia
@nbinclude(foo.ipynb; softscope=true)

```
