julia: create and use a local package without Inte

2019-03-15 06:35发布

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 read this link but I still dont know how to do it. I want to create a local package with a jl file and use it in my own local project with this code: using MyPackage.

Could someone help me?

标签: package julia
1条回答
走好不送
2楼-- · 2019-03-15 06:54

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.

查看更多
登录 后发表回答