# How to conditionally load code with package extensions

**URL:** <https://discourse.julialang.org/t/how-to-conditionally-load-code-with-package-extensions/101578>\
**Category:** General Usage\
**Tags:** question, package-manager\
**Created:** [July 13, 2023, 3:27pm UTC](https://discourse.julialang.org/t/how-to-conditionally-load-code-with-package-extensions/101578 "2023-07-13T15:27:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 13, 2023, 3:27pm UTC](https://discourse.julialang.org/t/how-to-conditionally-load-code-with-package-extensions/101578/1 "2023-07-13T15:27:35Z")

</div>

I have been going through documentation and examples on GitHub to learn how to conditionally load code. I keep running into issues. I think I would benefit from a MWE. I created a repo for this purpose [here](https://github.com/itsdfish/PackageExtensionsExample.jl). I would like some code load conditionally when Distributions.jl is also loaded. Here is what I have done so far:

```julia
using PackageExtensionsExample
using Distributions

```

When I load Distributions, I receive the following error:

> **Summary**
>
> [Info: Precompiling DistributionsExt [e012062e-4c15-5e28-8fa8-ee6d8c117614]  
> ERROR: LoadError: ArgumentError: Package Base does not have Distributions in its dependencies:
> 
> - You may have a partially installed environment. Try `Pkg.instantiate()`  
> to ensure all packages in the environment are installed.
> - Or, if you have Base checked out for development and have  
> added Distributions as a dependency but haven’t updated your primary  
> environment’s manifest file, try `Pkg.resolve()`.
> - Otherwise you may need to report an issue with Base  
> Stacktrace:  
> [1] macro expansion  
> @ ./loading.jl:1634 [inlined]  
> [2] macro expansion  
> @ ./lock.jl:267 [inlined]  
> [3] require(into::Module, mod::Symbol)  
> @ Base ./loading.jl:1611  
> [4] top-level scope  
> @ ~/.julia/dev/PackageExtensionsExample/ext/DistributionsExt.jl:1  
> [5] include  
> @ ./Base.jl:457 [inlined]  
> [6] include\_package\_for\_output(pkg::Base.PkgId, input::String, depot\_path::Vector{String}, dl\_load\_path::Vector{String}, load\_path::Vector{String}, concrete\_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing)  
> @ Base ./loading.jl:2045  
> [7] top-level scope  
> @ stdin:3  
> in expression starting at /home/dfish/.julia/dev/PackageExtensionsExample/ext/DistributionsExt.jl:1  
> in expression starting at stdin:3  
> ┌ Error: Error during loading of extension DistributionsExt of PackageExtensionsExample, use `Base.retry_load_extensions()` to retry.  
> │ exception =  
> │ 1-element ExceptionStack:  
> │ Failed to precompile DistributionsExt [e012062e-4c15-5e28-8fa8-ee6d8c117614] to “/home/dfish/.julia/compiled/v1.9/DistributionsExt/jl\_gb7Rqe”.  
> │ Stacktrace:  
> │ [1] error(s::String)  
> │ @ Base ./error.jl:35  
> │ [2] compilecache(pkg::Base.PkgId, path::String, internal\_stderr::IO, internal\_stdout::IO, keep\_loaded\_modules::Bool)  
> │ @ Base ./loading.jl:2296  
> │ [3] compilecache  
> │ @ ./loading.jl:2163 [inlined]  
> │ [4] \_require(pkg::Base.PkgId, env::Nothing)  
> │ @ Base ./loading.jl:1805  
> │ [5] \_require\_prelocked(uuidkey::Base.PkgId, env::Nothing)  
> │ @ Base ./loading.jl:1660  
> │ [6] \_require\_prelocked(uuidkey::Base.PkgId)  
> │ @ Base ./loading.jl:1658  
> │ [7] run\_extension\_callbacks(extid::Base.ExtensionId)  
> │ @ Base ./loading.jl:1255  
> │ [8] run\_extension\_callbacks(pkgid::Base.PkgId)  
> │ @ Base ./loading.jl:1290  
> │ [9] run\_package\_callbacks(modkey::Base.PkgId)  
> │ @ Base ./loading.jl:1124  
> │ [10] \_require\_prelocked(uuidkey::Base.PkgId, env::String)  
> │ @ Base ./loading.jl:1667  
> │ [11] macro expansion  
> │ @ ./loading.jl:1648 [inlined]  
> │ [12] macro expansion  
> │ @ ./lock.jl:267 [inlined]  
> │ [13] require(into::Module, mod::Symbol)  
> │ @ Base ./loading.jl:1611  
> │ [14] eval  
> │ @ ./boot.jl:370 [inlined]  
> │ [15] eval\_user\_input(ast::Any, backend::REPL.REPLBackend, mod::Module)  
> │ @ REPL ~/julia-1.9.1/share/julia/stdlib/v1.9/REPL/src/REPL.jl:153  
> │ [16] repl\_backend\_loop(backend::REPL.REPLBackend, get\_module::Function)  
> │ @ REPL ~/julia-1.9.1/share/julia/stdlib/v1.9/REPL/src/REPL.jl:249  
> │ [17] start\_repl\_backend(backend::REPL.REPLBackend, consumer::Any; get\_module::Function)  
> │ @ REPL ~/julia-1.9.1/share/julia/stdlib/v1.9/REPL/src/REPL.jl:234  
> │ [18] run\_repl(repl::REPL.AbstractREPL, consumer::Any; backend\_on\_current\_task::Bool, backend::Any)  
> │ @ REPL ~/julia-1.9.1/share/julia/stdlib/v1.9/REPL/src/REPL.jl:379  
> │ [19] run\_repl(repl::REPL.AbstractREPL, consumer::Any)  
> │ @ REPL ~/julia-1.9.1/share/julia/stdlib/v1.9/REPL/src/REPL.jl:365  
> │ [20] (::Base.var"#1017#1019"{Bool, Bool, Bool})(REPL::Module)  
> │ @ Base ./client.jl:421  
> │ [21] #invokelatest#2  
> │ @ ./essentials.jl:816 [inlined]  
> │ [22] invokelatest  
> │ @ ./essentials.jl:813 [inlined]  
> │ [23] run\_main\_repl(interactive::Bool, quiet::Bool, banner::Bool, history\_file::Bool, color\_set::Bool)  
> │ @ Base ./client.jl:405  
> │ [24] exec\_options(opts::Base.JLOptions)  
> │ @ Base ./client.jl:322  
> │ [25] \_start()  
> │ @ Base ./client.jl:522  
> └ @ Base loading.jl:1261

Running resolve and instantiate do not solve the issue. Any help would be greatly apprecaited.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 13, 2023, 5:16pm UTC](https://discourse.julialang.org/t/how-to-conditionally-load-code-with-package-extensions/101578/2 "2023-07-13T17:16:00Z")

</div>

I was able to figure out how to conditionally load an imported function from Distributions based on a custom type: `logpdf(d::MyType)`.

I was hoping that I could also functions and types not defined in another package. I tried using `export`, but to no avail. Is that possible?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 13, 2023, 5:46pm UTC](https://discourse.julialang.org/t/how-to-conditionally-load-code-with-package-extensions/101578/3 "2023-07-13T17:46:20Z")

</div>

As far as I can tell, it’s not possible to export a function only defined in the extension module. It has to be defined in a different module and imported. The example in the repo now works. I will update it if someone knows a simpler solution.
