如何在Julia中找到包裹的路径? [英] How to find the path of a package in Julia?

查看:135
本文介绍了如何在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子模块


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.

这与 pathof文档

返回用于导入模块mm.jl文件的路径,如果不是从包中导入m,则不返回任何内容.

Return the path of the m.jl file that was used to import module m, or nothing if m 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通过清单解析模块位置. 如果您在加载模块后更改了清单, 那么加载的模块实际上仍将引用旧位置,但是清单(因此清单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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆