julia: create and use a local package without Internet

前端 未结 1 1124
梦谈多话
梦谈多话 2021-02-04 05:03

I am trying to create a package of the julia language and use it in a project.
For now I have only a jl file, I dont know how to create a package with it.

I have rea

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 05:41

    You should put the file in

    ~/.julia/v0.X/MyPackage/src/MyPackage.jl

    Where ~ is your home directory and X is the version number of Julia that you are using. X will be 3, unless you are on the development or nightly version of Julia, in which case it will be 4.

    Also note that for this to work the file MyPackage.jl should define the module MyPackage and export the definitions you want to have available after calling using MyPackage.

    To automate the creation of this structure to you can call Pkg.generate("MyPackage", "MIT"), where MIT can be replaced by another supported default licence. This will create the folder in the correct place and set up the module structure for you. Then you just need to incorporate your code into that structure.


    EDIT

    Here is an example of two possible contents for the file ~/.julia/v0.3/MyPackage/src/MyPackage.jl:

    module MyPackage
    
    function test()
        1
    end
    
    end  # module
    

    and

    module MyPackage
    
    export test
    
    function test()
        1
    end
    
    end  # module
    

    In the first case I have not exported anything. Thus, when calling using MyPackage only the module MyPackage itself would be made available to the user. If I wanted to use the test function, I would have to use the fully qualified name: MyPackage.test().

    In the second case I chose to export the function test. This happened on the line export test. Now when I call using MyPackage, both the module MyPackage and the function test are available to the user. I do not need to use a fully qualified name to access test anymore: test() will work.

    0 讨论(0)
提交回复
热议问题