茱莉亚:在没有互联网的情况下创建和使用本地软件包 [英] julia: create and use a local package without Internet

查看:85
本文介绍了茱莉亚:在没有互联网的情况下创建和使用本地软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个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的版本号.除非您使用的是Julia的开发版或夜间版本,否则X将为3,在这种情况下,它将为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.

这篇关于茱莉亚:在没有互联网的情况下创建和使用本地软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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