julia:创建和使用没有 Internet 的本地包 [英] julia: create and use a local package without Internet

查看:21
本文介绍了julia:创建和使用没有 Internet 的本地包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 julia 语言包并在项目中使用它.
目前我只有一个 jl 文件,我不知道如何用它创建一个包.

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.

我已阅读此链接,但我仍然不知道该怎么做它.我想用 jl 文件创建一个本地包,并在我自己的本地项目中使用此代码:using MyPackage.

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.

有人可以帮我吗?

推荐答案

你应该把文件放在

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

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

其中 ~ 是您的主目录,X 是您正在使用的 Julia 的版本号.X 将为 3,除非您使用的是 Julia 的开发版本或夜间版本,在这种情况下它将为 4.

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.

还请注意,要使其正常工作,文件 MyPackage.jl 应定义模块 MyPackage 并导出您希望在使用 MyPackage 调用后可用的定义.

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.

要自动创建此结构,您可以调用 Pkg.generate("MyPackage", "MIT"),其中 MIT 可以替换为另一个受支持的默认许可证.这将在正确的位置创建文件夹并为您设置模块结构.然后,您只需将代码合并到该结构中.

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.

以下是文件 ~/.julia/v0.3/MyPackage/src/MyPackage.jl 的两个可能内容示例:

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

module MyPackage

export test

function test()
    1
end

end  # module

在第一种情况下,我没有 export 编辑任何内容.因此,当调用 using MyPackage 时,只有模块 MyPackage 本身可供用户使用.如果我想使用 test 函数,我必须使用完全限定名称:MyPackage.test().

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().

在第二种情况下,我选择导出函数 test.这发生在 export test 行.现在,当我调用 using MyPackage 时,模块 MyPackage 和函数 test 都可供用户使用.我不再需要使用完全限定名称来访问 test: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.

这篇关于julia:创建和使用没有 Internet 的本地包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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