Change Package directory in Julia

前端 未结 2 1279
眼角桃花
眼角桃花 2020-12-02 23:38

I want to change the Package directory in Julia. The default is

\"~/.julia/v0.4\"

I want to move it to /opt/julia/v0.4/

相关标签:
2条回答
  • 2020-12-03 00:14

    Julia-v0.6 and before

    one can change julia's package directory by following these steps:

    1. run export JULIA_PKGDIR=/your/directory in shell(or manually add a new environment variable JULIA_PKGDIR on windows)
    2. run Pkg.init() in julia to initialize a new package system
    3. copy REQUIRE from old directory to the new one
    4. run Pkg.resolve() in julia

    Julia-v0.7+

    The "Package directory" in the new package manager is called DEPOT_PATH, it can be changed by adding an environment variable JULIA_DEPOT_PATH:

    JULIA_DEPOT_PATH=./test julia
    
    julia> DEPOT_PATH
    1-element Array{String,1}:
     "./test"
    
    (v0.7) pkg> add JSON2
       Cloning default registries into /Users/gnimuc/test/registries
    

    With the new package manager, we are able to create isolated projects in any directory we want instead of having a single giant package directory. Every project contains a Project.toml and a Manifest.toml if it has some dependencies. These two files record and keep tracking the environment of the project.

    UPDATE

    The following info might be obsoleted. I highly recommend to use PkgTemplates.jl for generating projects in Julia-v1.0+.

    Generate a new project

    We can generate a new project in any folder, but we must cd to the project folder to using the project. The (v0.7) below shows we're still in the default environment, so we cannot use the newly generated project:

    (v0.7) pkg> generate ./MyNewProject
    Generating project MyNewProject:
        ./MyNewProject/Project.toml
        ./MyNewProject/src/MyNewProject.jl
    
    julia> using MyNewProject
    ERROR: ArgumentError: Module MyNewProject not found in current path.
    Run `Pkg.add("MyNewProject")` to install the MyNewProject package.
    Stacktrace:
     [1] require(::Module, ::Symbol) at ./loading.jl:868
    

    If we cd to the project folder and activate the environment, then we can using our new project without any problems:

    shell> cd MyNewProject/
    /Users/gnimuc/MyNewProject
    
    (v0.7) pkg> activate .
    
    (MyNewProject) pkg> 
    
    julia> using MyNewProject
    

    I think that's the big difference between the new package manager and the old one. In short, we need to explicitly activate our unregistered project/package.

    Download and init someone else's project

    According to the doc, we can add an unregistered package/project via add command:

    (HelloWorld) pkg> add https://github.com/fredrikekre/ImportMacros.jl
    

    This command adds the target package as a dependency of our current project. In this example, we added ImportMacros in HelloWorld's Manifest.toml. What if we just use it as a top-level project? To add it to the default environment (v0.7)? no, we don't need to. The answer is we can directly download the code, cd to the folder and run instantiate in the pkg mode:

    shell> git clone https://github.com/Gnimuc/GLTF.jl GLTF
    Cloning into 'GLTF'...
    remote: Counting objects: 286, done.
    remote: Compressing objects: 100% (56/56), done.
    remote: Total 286 (delta 73), reused 103 (delta 59), pack-reused 167
    Receiving objects: 100% (286/286), 62.16 KiB | 46.00 KiB/s, done.
    Resolving deltas: 100% (135/135), done.
    
    shell> cd GLTF
    
    pkg> activate .
    
    (GLTF) pkg> instantiate
      Updating registry at `~/.julia/registries/General`
      Updating git-repo `https://github.com/JuliaRegistries/General.git`
    

    The new package manager is great! We neither need "include before using" nor make everything as a package just for using it. We have full-featured "Project" now!

    0 讨论(0)
  • 2020-12-03 00:19

    Julia only way:

    julia> ENV["JULIA_PKGDIR"] = "E:\\Julia-0.6.0\\portable"
    "E:\\Julia-0.6.0\\portable"
    
    julia> ENV["JULIA_PKGDIR"]
    "E:\\Julia-0.6.0\\portable"
    
    julia> Pkg.init()
    INFO: Initializing package repository E:\Julia-0.6.0\portable\v0.6
    INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl
    

    However, the cache dir is still pointing to the old folder, so I checked why that is and figured it out:

    julia> Base.LOAD_CACHE_PATH
    1-element Array{String,1}:
     "C:\\Users\\kung\\.julia\\lib\\v0.6"
    
    julia> Pkg.__init__()
    2-element Array{String,1}:
     "E:\\Julia-0.6.0\\portable\\lib\\v0.6"
     "C:\\Users\\kung\\.julia\\lib\\v0.6"
    
    julia> pop!(Base.LOAD_CACHE_PATH)
    "C:\\Users\\kung\\.julia\\lib\\v0.6"
    
    julia> Base.LOAD_CACHE_PATH
    1-element Array{String,1}:
     "E:\\Julia-0.6.0\\portable\\lib\\v0.6"
    

    As simple function:

    function set_julia_dir(dir::String)
        ENV["JULIA_PKGDIR"] = dir
        Pkg.init()
        Pkg.__init__()
        pop!(Base.LOAD_CACHE_PATH)
    end
    
    set_julia_dir("E:\\Julia-0.6.0\\portable")
    
    0 讨论(0)
提交回复
热议问题