# How to run my version of a function from a package

**URL:** <https://discourse.julialang.org/t/how-to-run-my-version-of-a-function-from-a-package/117073>\
**Category:** New to Julia\
**Created:** [July 15, 2024, 8:46pm UTC](https://discourse.julialang.org/t/how-to-run-my-version-of-a-function-from-a-package/117073 "2024-07-15T20:46:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sideyt](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@sideyt](https://discourse.julialang.org/u/sideyt)\
**Post date:** [July 15, 2024, 8:46pm UTC](https://discourse.julialang.org/t/how-to-run-my-version-of-a-function-from-a-package/117073/1 "2024-07-15T20:46:38Z")

</div>

I have translated SAMIN (optimize) in the Optim package to another language (where it is needed) and need to add print statements to the Julia code for comparison. This is the code I wish to run:

> <https://github.com/JuliaNLSolvers/Optim.jl/blob/master/src/multivariate/solvers/constrained/samin.jl>

I have placed samin.jl in my own directory (separate from the julia distribution). When I type: include(“samin.jl”) in the REPL I get:  
**ERROR:** LoadError: UndefVarError: `@with_kw` not defined

Stacktrace:

[1] top-level scope

@ :0

[2] **#macroexpand#66**

@ ./expr.jl:122 [inlined]

[3] **macroexpand**

@ ./expr.jl:120 [inlined]

[4] **docm(source::LineNumberNode, mod::Module, meta::Any, ex::Any, define::Bool)**

@ Base.Docs ./docs/Docs.jl:555

[5] **var"@doc"(**::LineNumberNode, ::Module, ::String, ::Vararg{Any}**)**

@ Core ./boot.jl:541

[6] **include(fname::String)**

@ Base.MainInclude ./client.jl:489

[7] top-level scope

@ REPL[1]:1

in expression starting at /Users/stimmins/julia/samin.jl:19

in expression starting at /Users/stimmins/julia/samin.jl:19

Being new I need detailed instructions please - it has something to do with defaults - but how to remedy? Thank you.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 15, 2024, 10:28pm UTC](https://discourse.julialang.org/t/how-to-run-my-version-of-a-function-from-a-package/117073/2 "2024-07-15T22:28:21Z")

</div>

Most .jl files in packages are not supposed to be evaluated in isolation, but within another primary file. In Optim.jl/src/Optim.jl, there is a line `include("multivariate/solvers/constrained/samin.jl")` occurring far after a line `import Parameters: @with_kw, @unpack`, among other imports and `include`s. To make your `println` version work, you would need to replicate the minimum imports and `include`s that the original needed to work.

I don’t think it’s worth manually doing that because at worst case, you need every preceding line and you might as well fork the whole package at that point. Looking at local variables while debugging is a typical alternative, but it can be too hands-on and slow compared to a run-once-print-all approach. You could load the Optim package and `@eval Optim ...` modified functions right into the active Optim module, and it will work like the original in most cases. Make sure to change the function name or dispatch on a custom-typed argument so you’re not replacing the original code.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [July 16, 2024, 1:42am UTC](https://discourse.julialang.org/t/how-to-run-my-version-of-a-function-from-a-package/117073/3 "2024-07-16T01:42:00Z")

</div>

Another idea is maybe to instead of `]add` Optim.jl to your environment you `]dev` it instead. That creates a copy of the fully module in your `.julia/dev` (not quite sure about this path, haven’t double checked). There you can modify the code, i.e. add the `println`s. Then you can just run a test code normally.
