I am trying to find the path to a specific package in Julia. I do not know exactly where it is and I want to use the appropriate path. Is there a function that does this in Juli
Once you have it loaded yuou have a Module
object.
If you have module object you can use pathof
to find it.
julia> using DataFrames
julia> pathof(DataFrames)
"/Users/oxinabox/.julia/packages/DataFrames/cdZCk/src/DataFrames.jl"
julia> pathof(DataFrames.PooledArrays)
"/Users/oxinabox/.julia/packages/PooledArrays/yiLq3/src/PooledArrays.jl"
If we were a bit broader and wanted the path to a module that wasn't a package, but was either loaded directly, or a submodule, than pathof
won't work.
For example LibPQ.jl has a Errors
submodule
julia> using LibPQ
julia> pathof(LibPQ)
"/Users/oxinabox/.julia/packages/LibPQ/SFs6f/src/LibPQ.jl"
julia> typeof(LibPQ.Errors)
Module
julia> pathof(LibPQ.Errors)
the output was nothing
.
This is as per the pathof documentation
Return the path of the
m.jl
file that was used to import modulem
, or nothing ifm
was not imported from a package.
If you want to track that down there is a trick.
All modules in julia (except baremodules
) automatically contain a defination for there own eval
function.
We can look up the location of this function from the method table.
julia> module_file(modu) = String(first(methods(getfield(modu, :eval))).file)
module_file (generic function with 1 method)
julia> module_file(LibPQ)
"/Users/oxinabox/.julia/packages/LibPQ/SFs6f/src/LibPQ.jl"
julia> module_file(LibPQ.Errors)
"/Users/oxinabox/.julia/packages/LibPQ/SFs6f/src/exceptions.jl
Other than baremodules
and modules that are not packages, there is one other case where they disagree.
pathof
resolves module location via the Manifest.
If you change the manfiest after loading a module,
then the module that is loaded will still actually refer to the old location, but the Manifest, and thus pathof
will think it is at the new location.
(11) pkg> dev --local LibPQ
Cloning git-repo `https://github.com/invenia/LibPQ.jl.git`
Resolving package versions...
Updating `~/temp/11/Project.toml`
[194296ae] ~ LibPQ v1.5.0 ⇒ v1.5.0 `dev/LibPQ`
Updating `~/temp/11/Manifest.toml`
[194296ae] ~ LibPQ v1.5.0 ⇒ v1.5.0 `dev/LibPQ`
Building LibPQ → `~/temp/11/dev/LibPQ/deps/build.log`
julia> pathof(LibPQ)
"/Users/oxinabox/temp/11/dev/LibPQ/src/LibPQ.jl"
julia> module_file(LibPQ)
"/Users/oxinabox/.julia/packages/LibPQ/SFs6f/src/LibPQ.jl"
pathof
is giving what is arguably the wrong answer (this is true for julia 1.5 at least I suspect it might change in the future.)
but module_file
, because it looks at what code is actually loaded and records that location at load time, gives the right answer.
Julia's standard library (Base) provides a find_package
function which works as follows:
julia> Base.find_package("Random")
"/Applications/Julia-1.3.app/Contents/Resources/julia/share/julia/stdlib/v1.3/Random/src/Random.jl"
julia> Base.find_package("JSON")
"/Users/logankilpatrick/.julia/packages/JSON/d89fA/src/JSON.jl"
You can use pathof
to find the location of an imported module (and package if the module was in a package)
julia> using Random
julia> pathof(Random)
"C:\\Julia-1.4.1\\share\\julia\\stdlib\\v1.4\\Random\\src\\Random.jl"