【问题标题】:Inheritance in Julia environmentsJulia 环境中的继承
【发布时间】:2023-01-19 22:16:32
【问题描述】:

我不清楚在 Julia 中使用 Pkg.generate() 创建的环境是否完全隔离,或者它们是否继承了分层目录树中目录中安装的一些模块/包。如果是这种情况,有什么办法可以从一个完全空的环境开始?我认为 Base 环境是强制性的,对吧?谢谢。

【问题讨论】:

    标签: julia


    【解决方案1】:

    Julia 中的环境是堆叠的——有一个默认环境(以 Julia 版本命名,例如 @1.8 代表 Julia 1.8.x),默认情况下可以从任何活动环境访问。文档的相关部分可以在here 找到。

    从那里引用:

    第三种也是最后一种环境是通过叠加多个环境来组合其他环境,使每个环境中的包在单个复合环境中可用。这些复合环境称​​为环境堆栈。 Julia LOAD_PATH 全局定义了一个环境堆栈——Julia 进程运行的环境。如果您希望您的 Julia 进程只能访问一个项目或包目录中的包,请将其设为 LOAD_PATH 中的唯一条目。

    要查看实际效果:

    julia> Base.LOAD_PATH
    3-element Vector{String}:
     "@"
     "@v#.#"
     "@stdlib"
    

    这里 @v#.# 是默认环境,@stdlib 顾名思义是标准库(例如 [at least for now!] DelimitedFilesStatistics)。 LOAD_PATH 的帮助条目提供了一些更详细的信息:

    help?> LOAD_PATH
    search: LOAD_PATH
    
      LOAD_PATH
    
      An array of paths for using and import statements to consider as project environments or package directories when loading code. It is populated based on the JULIA_LOAD_PATH environment variable if set;
      otherwise it defaults to ["@", "@v#.#", "@stdlib"]. Entries starting with @ have special meanings:
    
        •  @ refers to the "current active environment", the initial value of which is initially determined by the JULIA_PROJECT environment variable or the --project command-line option.
    
        •  @stdlib expands to the absolute path of the current Julia installation's standard library directory.
    
        •  @name refers to a named environment, which are stored in depots (see JULIA_DEPOT_PATH) under the environments subdirectory. The user's named environments are stored in ~/.julia/environments so
           @name would refer to the environment in ~/.julia/environments/name if it exists and contains a Project.toml file. If name contains # characters, then they are replaced with the major, minor and
           patch components of the Julia version number. For example, if you are running Julia 1.2 then @v#.# expands to @v1.2 and will look for an environment by that name, typically at
           ~/.julia/environments/v1.2.
    
      The fully expanded value of LOAD_PATH that is searched for projects and packages can be seen by calling the Base.load_path() function.
    

    如果需要,您可以从 LOAD_PATH 中删除所有内容:

    C:>set JULIA_LOAD_PATH=""
    
    C:>julia -q
    
    
    julia> Base.LOAD_PATH
    1-element Vector{String}:
     """"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多