# Launching examples from a Julia package

**URL:** https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814
**Category:** General Usage
**Tags:** question, jupyter
**Created:** [July 12, 2017, 4:35pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814 "2017-07-12T16:35:22Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 12, 2017, 4:35pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/1 "2017-07-12T16:35:22Z")

</div>

I am trying to provide a convenience function for the users of my package that launchs Jupyter notebook examples from the command line without depending on IJulia:

```julia
module Foo

# ...

function examples()
  using IJulia
  notebook(dir=joinpath(@ __DIR__ ,"examples"))
end

end

```

The user would then launch the examples with “Foo.examples()” from the Julia prompt. The code above doesn’t work because we are not allowed to have `using` statements inside of functions. Is there any solution around this?

I believe that we would benefit from having reproducible examples in Julia packages via a standardized process. Is there anything happening with this regards already?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [July 12, 2017, 4:50pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/2 "2017-07-12T16:50:01Z")

</div>

You could use eval: `@eval using IJulia`.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 12, 2017, 5:02pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/3 "2017-07-12T17:02:14Z")

</div>

Thank you @mauro3. I am getting a strange error message now saying that `notebook()` is not a valid method:

```julia
ERROR: MethodError: no method matching notebook()
The applicable method may be too new: running in world age 21816, while current world is 21820.
Closest candidates are:
  notebook(; dir, detached) at /home/juliohm/.julia/v0.6/IJulia/src/IJulia.jl:104 (method too new to be called from this world context.)

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [July 12, 2017, 5:17pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/4 "2017-07-12T17:17:42Z")

</div>

Yep. Maybe use `invokelatest` or also `@eval`.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 12, 2017, 5:22pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/5 "2017-07-12T17:22:03Z")

</div>

Perfect @mauro3, the final solution for future reference:

```julia
module Foo
function examples()
  @eval using IJulia
  @eval notebook(dir=Pkg.dir("Foo","examples"))
end
end

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [July 12, 2017, 9:31pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/6 "2017-07-12T21:31:10Z")

</div>

> [@juliohm](#):
>
> joinpath(Pkg.dir(“Foo”),“examples”)

Pkg.dir has a built in joinpath, so this can be elegantly written as  
`Pkg.dir("Foo", "examples")`

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 12, 2017, 9:32pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/7 "2017-07-12T21:32:35Z")

</div>

Thanks @baggepinnen, that is definitely more elegant 🙂

Will update the answer and mark as solved.

---

<div class="post-metadata">

### Author: ![tkelman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkelman/32/692_2.png) [@tkelman](https://discourse.julialang.org/u/tkelman)
#### Post date: [July 13, 2017, 12:38pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/8 "2017-07-13T12:38:45Z")

</div>

The `@ __DIR__ ` you originally had is better than using `Pkg.dir`, as the former will work if the package is loaded from somewhere else on `LOAD_PATH`.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 13, 2017, 2:51pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/9 "2017-07-13T14:51:33Z")

</div>

Thank you @tkelman, I will unroll the changes and use `@ __DIR__ ` again, I always forget that `Pkg.dir()` is not portable.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 13, 2017, 2:54pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/10 "2017-07-13T14:54:35Z")

</div>

@tkelman, actually `@ __DIR__ ` isn’t defined in the REPL? I tried calling the function after loading the package, and `@ __DIR__ ` is `nothing`.

---

<div class="post-metadata">

### Author: ![tkelman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkelman/32/692_2.png) [@tkelman](https://discourse.julialang.org/u/tkelman)
#### Post date: [July 13, 2017, 3:01pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/11 "2017-07-13T15:01:46Z")

</div>

Yeah depends whether you intend to write the code down in a .jl file that users will include, or as an example that they will copy-paste in the REPL. I think `@ __DIR__ ` was changed recently to behave as `pwd()` when used in the REPL, but that’s not exactly what you want for copy-pasted code.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 13, 2017, 3:08pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/12 "2017-07-13T15:08:31Z")

</div>

In that case, I will keep using `Pkg.dir()`, it should work in nearly 100% of the cases.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [July 13, 2017, 4:53pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/13 "2017-07-13T16:53:49Z")

</div>

Why not

```julia
module Foo
using IJulia
function examples()
  notebook(dir=Pkg.dir("Foo","examples"))
end
end

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 13, 2017, 5:13pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/14 "2017-07-13T17:13:22Z")

</div>

@PetrKryslUCSD, I don’t want to depend on IJulia, that is why we delay the evaluation inside of the function. So everyone that needs to run the examples, will install IJulia.

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [July 13, 2017, 5:21pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/15 "2017-07-13T17:21:22Z")

</div>

> [@juliohm](#):
>
> module Foo  
> function examples()  
> @eval using IJulia  
> @eval notebook(dir=Pkg.dir(“Foo”,“examples”))  
> end  
> end

> [@juliohm](#):
>
> actually @ **DIR** isn’t defined in the REPL

Untried, but maybe that is avoidable by extracting the macro out of the `@eval` portion

```julia
module Foo
function examples()
  path = joinpath(@ __DIR__ , "examples")
  @eval using IJulia
  @eval notebook(dir=path)) 
end
end

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 13, 2017, 5:29pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/16 "2017-07-13T17:29:26Z")

</div>

Thank you @Evizero, unfortunately it doesn’t work, it says `path` is undefined?

---

<div class="post-metadata">

### Author: ![adamslc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adamslc/32/3452_2.png) [@adamslc](https://discourse.julialang.org/u/adamslc)
#### Post date: [July 13, 2017, 5:53pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/17 "2017-07-13T17:53:27Z")

</div>

Maybe try

```julia
@eval notebook(dir=$path)

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 13, 2017, 6:08pm UTC](https://discourse.julialang.org/t/launching-examples-from-a-julia-package/4814/18 "2017-07-13T18:08:00Z")

</div>

There are currently two possible solutions thanks to @mauro3, @Evizero, @adamslc and @tkelman:

Solution a)

```julia
function examples()
  @eval using IJulia
  opennotebook(dir) = notebook(dir=dir)
  Base.invokelatest(opennotebook, joinpath(@ __DIR__ ,"..","examples"))
end

```

Solution b)

```julia
function examples()
  path = joinpath(@ __DIR__ ,"..","examples")
  @eval using IJulia
  @eval notebook(dir=$path)
end

```

I am biased towards solution b), will update the answer in the thread.
