jjjjjj
jjjjjj

Reputation: 1172

edit source code (and recompile?) julia package

I want to edit the source code to a particular Julia package. To make it concrete, let's say I want to add a new distribution to the Distributions.jl package. However, I'm having difficulty finding where the Julia source code is (and imagine once I find it, I may have to rebuild/recompile Julia?). I've added the package with Pkg.add("Distributions.jl") but can't find it on my computer (MacOS).

For example, if I wanted to edit a function in Python's numpy package, (using Anaconda) I'd navigate to /Users/MYUSER/anaconda/lib/python2.7/site-packages/numpy and edit the source there. How do I do this in Julia, in particular for the Distributions.jl package? Can you point a novice to any references?

Upvotes: 3

Views: 3459

Answers (4)

Assaf
Assaf

Reputation: 316

Some of the answers above are outdated / partial / didn't work for me so I thought I'd give my two cents here as I had this exact need recently (using Julia 1.5.3).

First of all and unfortunately, Pkg.dir() is deprecated and gets a warning when used.

To find the path where a module/package code resides you can use the Base.pathof(::Module) function, for example:

julia> pathof(HTTP)
"C:\\Users\\User\\.julia\\packages\\HTTP\\IAI92\\src\\HTTP.jl"

To edit a module's code you can use the REPL, for example:

julia> edit(HTTP)

will open your editor at the HTTP module's souce code.

Alternatively, when using VSCode you can right click on a function call and use the Go to Definition (F12) context menu option and open to the source code in your editor (Atom has an equivalent Go to Declaration option but it did not work for me).

Note that you can resort to the quick-and-dirty method and just edit the code where you find it or you use a much cleaner way offered by the Julia Pkg module:

pkg> develop --local Example

This will create a git clone of the Example package under your current project root so you can work on a separate local copy of the package code in a development environment. To stop working with the dev module use:

pkg> free Example

For more info refer to the Julia Pkg module documentation

Please keep in mind that (as already stated in the answers above) Julia pre-compiles modules on startup only, so unless you are using Revise.jl as pointed-out by Liso above, every change to a module's code requires restarting Julia in order to recompile the changed module. this can be done by typing

julia> exit()

and running the using <Module Name> statement again.

Edit: this question is closely related to: How to find the path of a package in Julia

Edit: VSCode's Julia extension has an option for loading Revise.jl when starting the Julia REPL, the default for this option is true.

Upvotes: 4

Liso
Liso

Reputation: 2260

If you want to edit particular method then you could use @edit macro as well:

julia> @edit Distributions.Levy()

If you use supported editor you'll be in line where method is defined. In vim's statusbar I could see "~/.julia/v0.6/Distributions/src/univariate/continuous/levy.jl" 102L, 2751C (so definition in my version starts at line 102 of file levy.jl)

You could set editor which is supported (for example atom, sublime, kate, gedit, emacs, notepad, ... by:

julia> ENV["JULIA_EDITOR"] = "vim";

and you could do this in ~/.juliarc.jl (or by export JULIA_EDITOR=vim in .bashrc or what your shell allow).

Which editors are supported you could see using (warning: you have to analyse source code!):

julia> @edit edit("")

Or which will be supported probably looking at https://github.com/JuliaLang/julia/blob/master/base/interactiveutil.jl

edit:

Ad recompile: I didn't test it but Revise.jl seems very useful!

Upvotes: 5

Chris Rackauckas
Chris Rackauckas

Reputation: 19162

Pkg.dir("Distributions")
#Returns "C:\\Users\\Chris\\.julia\\v0.6\\Distributions"

You can edit the file directly and execute it using Juno's inline evaluation and that will update it inside the module, so no restarting is necessary like that. Here's a video I made awhile back on Julia package development.

Upvotes: 5

jjjjjj
jjjjjj

Reputation: 1172

I've found the source code at /Users/MYUSER/.julia/v0.6/Distributions/src/. It was a hidden folder that I didn't see originally...

Upvotes: 2

Related Questions