如何在 Julia 中找到包的路径? [英] How to find the path of a package in Julia?
问题描述
我正在尝试在 Julia 中查找特定包的路径.我不知道它到底在哪里,我想使用适当的路径.Julia 中有没有这样的功能?
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 Julia?
推荐答案
一旦你加载了它,你就有了一个 Module
对象.
Once you have it loaded yuou have a Module
object.
如果你有模块对象,你可以使用 pathof
来找到它.
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"
如果我们稍微宽一点,并且想要一个不是包但直接加载或子模块的模块的路径,那么 pathof
将不起作用.
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.
例如 LibPQ.jl 有一个 Errors
子模块
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)
输出是 nothing
.
这是根据 <代码>路径文档
This is as per the pathof
documentation
返回用于导入模块 m
的 m.jl
文件的路径,如果 m
不是从包.
Return the path of the
m.jl
file that was used to import modulem
, or nothing ifm
was not imported from a package.
如果你想追踪它,有一个窍门.julia 中的所有模块(除了 baremodules
)都会自动包含对自己的 eval
函数的定义.我们可以从方法表中查找这个函数的位置.
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
除了 baremodules
和不是包的模块之外,还有另一种情况他们不同意.
Other than baremodules
and modules that are not packages, there is one other case where they disagree.
pathof
通过 Manifest 解析模块位置.如果在加载模块后更改 manfiest,那么加载的模块实际上仍然会引用旧位置,但 Manifest,因此 pathof
会认为它位于新位置.
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
给出了可以说是错误的答案(对于 julia 1.5 来说是这样,至少我怀疑它将来可能会改变.)但是 module_file
,因为它查看实际加载的代码并在加载时记录该位置,所以给出了正确的答案.
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 中找到包的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!