# How to correctly write package extension code?

**URL:** <https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980>\
**Category:** General Usage\
**Tags:** package-extensions\
**Created:** [June 7, 2023, 1:05am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980 "2023-06-07T01:05:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [June 7, 2023, 1:05am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/1 "2023-06-07T01:05:33Z")

</div>

For example, I have a main package called `NeuralNetworks`, and an extension called `MNIST`. Now I want to load `MNIST` if package [`MLDatasets.jl`](https://github.com/JuliaML/MLDatasets.jl) is already loaded.

I wrote [the following code in `NeuralNetworks`](https://github.com/singularitti/NeuralNetworks.jl/blob/424c8f4d30668bdd4127a0f0789f9b00168236c5/src/NeuralNetworks.jl#L12-L16):

```julia
function loaddata end

if !isdefined(Base, :get_extension)
    include("../ext/MNIST.jl")
end

```

where function `loaddata` is what I want to extend in the extension.

And I wrote [the following code in `MNIST`](https://github.com/singularitti/NeuralNetworks.jl/blob/424c8f4d30668bdd4127a0f0789f9b00168236c5/ext/MNIST.jl):

```julia
module MNIST

using MLDatasets: MLDatasets
if isdefined(Base, :get_extension)
    using NeuralNetworks
    import NeuralNetworks: loaddata
else
    using ..NeuralNetworks
    import ..NeuralNetworks: loaddata
end

export loaddata

function loaddata(type=:train)
    ...
end

end

```

My [`Project.toml` looks like](https://github.com/singularitti/NeuralNetworks.jl/blob/424c8f4d30668bdd4127a0f0789f9b00168236c5/Project.toml#L12-L16):

```toml
[weakdeps]
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"

[extensions]
MNIST = "MLDatasets"

```

Now I try to use the code:

```julia
In [1]: using NeuralNetworks: loaddata

In [2]: methods(loaddata)
# 0 methods for generic function "loaddata" from NeuralNetworks

In [3]: using MLDatasets

In [4]: methods(loaddata)
# 0 methods for generic function "loaddata" from NeuralNetworks

```

As you see, even if `MLDatasets` is loaded, `loaddata` is still not extended. What is wrong with my code? Thanks!

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [June 7, 2023, 1:43am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/2 "2023-06-07T01:43:18Z")

</div>

I’ve never got a package extension to load correctly when developing inside VSCode (would love to see if anyone has a fix for this!) - is that what you’re using for this? When I run your example in a terminal, it works for me:

```julia
julia> using MLDatasets

julia> methods(loaddata)
# 2 methods for generic function "loaddata" from NeuralNetworks:
 [1] loaddata()
     @ MNIST C:\Users\User\.julia\packages\NeuralNetworks\ZPIX0\ext\MNIST.jl:16
 [2] loaddata(type)
     @ MNIST C:\Users\User\.julia\packages\NeuralNetworks\ZPIX0\ext\MNIST.jl:16

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [June 7, 2023, 3:31am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/3 "2023-06-07T03:31:26Z")

</div>

Hi Daniel,  
I still cannot make it work. Both in VSCode and in a separate terminal. I wonder how you did this. Were you on commit [424c8f4](https://github.com/singularitti/NeuralNetworks.jl/commit/424c8f4d30668bdd4127a0f0789f9b00168236c5)?

```julia
In [1]: using NeuralNetworks

In [2]: using NeuralNetworks: loaddata

In [3]: methods(loaddata)
# 0 methods for generic function "loaddata" from NeuralNetworks

In [4]: using MLDatasets

In [5]: methods(loaddata)
# 0 methods for generic function "loaddata" from NeuralNetworks

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [June 7, 2023, 3:36am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/4 "2023-06-07T03:36:36Z")

</div>

Here’s my complete session:

```julia
C:\Users\User>julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.9.0 (2023-05-07)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

(@v1.9) pkg> activate --temp
  Activating new project at `C:\Users\User\AppData\Local\Temp\jl_QHuyqM`

(jl_QHuyqM) pkg> add https://github.com/singularitti/NeuralNetworks.jl, MLDatasets

julia> using NeuralNetworks

julia> using NeuralNetworks: loaddata

julia> methods(loaddata)
# 0 methods for generic function "loaddata" from NeuralNetworks

julia> using MLDatasets

julia> methods(loaddata)
# 2 methods for generic function "loaddata" from NeuralNetworks:
 [1] loaddata()
     @ MNIST C:\Users\User\.julia\packages\NeuralNetworks\ZPIX0\ext\MNIST.jl:16
 [2] loaddata(type)
     @ MNIST C:\Users\User\.julia\packages\NeuralNetworks\ZPIX0\ext\MNIST.jl:16

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [June 7, 2023, 3:53am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/5 "2023-06-07T03:53:24Z")

</div>

I get it. I did not install `MLDatasets` together alongside `NeuralNetworks`. `MLDatasets` is installed in my root path and `NeuralNetworks` is a local project. It seems that the new package has to be under the same environment as the main package. It kind of limits the usefulness of this functionality.

Thank you!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 7, 2023, 6:04am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/6 "2023-06-07T06:04:34Z")

</div>

> [@DanielVandH](#):
>
> I’ve never got a package extension to load correctly when developing inside VSCode (would love to see if anyone has a fix for this!)

I also have frequent troubles with extensions not loading in VSCode, I thought it might be due to Revise. Are you saying this works fine 100% of the time in the standard REPL?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [June 7, 2023, 6:31am UTC](https://discourse.julialang.org/t/how-to-correctly-write-package-extension-code/99980/7 "2023-06-07T06:31:50Z")

</div>

I should’ve said command prompt rather than terminal. Anywhere from inside VSCode never works for me when developing a package, but 100% of the time outside of VSCode.
