如何轻松地编写一个Julia应用包?


#1

优美的架构和语法使得Julia成为通用性编程语言中备受瞩目的新星。但其应用生态系统在丰富性和前沿性方面的不足阻碍了翘首以盼的看客们的步伐!那么,如何轻松有效地应用强大的Julia语言编写各领域应用包呢?特别是如何使得各领域内的非计算机专业的大牛和普通用户也能够轻松的编写应用包呢?
希望大牛和前辈指点:
(1)编写应用程序包的常用工具有哪些?
(2)如何简单地在各种操作系统上配置这些工具?
(3)具体的工作流程是怎样的?
(4)应用包的基本构成有哪些?其基本架构有哪些?
(5)整个流程中特别需要注意的地方是?

由于本人为非计算机专业小白,所提问题难免不够专业,请谅解~~~


#2

看一下Chris的这个简介,http://www.stochasticlifestyle.com/finalizing-julia-package-documentation-testing-coverage-publishing/


#3

那个博客是2016年的。新的Pkg模式刚出来,大家都在忙着升级。上面提到的问题大家都在摸索。

我会在接下来一段时间观察主要应用包的升级情况,并随时写一些文档放在博客上,链接到这里来。请关注。

目前为止最权威的信息在这个文档里,

更新很快,等稳定下来后适当机会我们可以汉化。


#4

征集FAQ问题。国内可能上不了Google Drive, 我全文转在这里供大家讨论。

Quick Glossary

Project – a directory with source code, tests, docs, etc - and a Project.toml file

Package – ABC.jl directory with src/ABC.jl which defines a module ABC, tests in tests/runtests.jl and Project.toml file

Environment – set of installed packages. There is one default, global environment. Each project can define its own environment (via the Project.toml file)

Registry – a repository which contains information about which packages exist. The default is https://github.com/JuliaRegistries/General.git, which replaces METADATA.jl from Julia 0.6

General

pkg> add Ab[TAB][TAB]
AbstractNumbers AbstractPlotting AbbrvKW AbstractDomains AbstractOperators AbstractTables AbstractTrees AbstractFFTs AbstractAlgebra AbaqusReader

Package management from a user perspective

  • How can I use the new “pkg>” prompt?

You can enter the “pkg>” prompt by typing “]” in the REPL. After doing so, the prompt should change from julia> to pkg> colored in light blue. To exit the “pkg>” prompt, simply press the backspace key.

  • Do I have to use the new interactive REPL or is there a programmatic way as well?

You can still write “Pkg.xxx” for programmatic use.

  • How do I install a package?

To install the package ABC you can either use the “pkg>” prompt:

pkg> add ABC

or call:

julia> Pkg.add(“ABC”)

  • How do I remove a package?

To remove the package ABC you can either use the “pkg>” prompt:

pkg> remove ABC

or

pkg> rm ABC

or call:

julia> Pkg.rm(“ABC”)

  • How do I update a package?

To update the package ABC you can either use the “pkg>” prompt:

pkg> update ABC

or

pkg> up ABC

or call:

julia> Pkg.update(“ABC”)

  • How do I list the installed packages?

To list the installed packages

  • How do I install a specific version of a package?

  • How do I install the latest master of a package ABC?

To install the master of package ABC you can use::

pkg> add ABC#master

  • Can I also easily install the latest versions of all the dependencies of X?

  • How do I switch to a different branch of a package?

  • Are all installed packages immutable?

  • Can I install multiple packages at once?

  • How do I update only a single package?

  • How does Julia know which packages exist / can be updates?

  • Which mechanism controls package version compatibility? REQUIRE or Project.toml?

  • How do I completely remove unused packages?

  • How can I check which version of a package gets loaded by Julia?

Updating from 0.6 to 0.7/1.0, from a user perspective

  • Where are packages downloaded / installed to in 1.0?

  • How do I change the package directory path in Julia 1.0 compared to Julia 0.6?

  • Does the new package manager support environments?

  • How do I change to another environment?

Project.toml and Manifest.toml

  • What is the layout of Project.toml? Which fields are optional, which ones required?

  • What is the layout of Manifest.toml?

  • Which of these should be added to a git repo?

Working on packages

  • I want to fix a bug - how can I work on a packages source code and make a pull request?

  • Can I control the location where the package source are getting checked out to?

  • In my package X, how can I have code executed only if package Y is installed? E.g. only provide image related functions when Images.jl is installed?

  • Do I have to include Test when I want to test my package?

  • How to I tag and publish a version of my package?

  • Do I have to use attabot for publishing my package?

Working on projects/applications

  • What is the difference between a package and a project/application from a package manager perspective?

Updating packages from 0.6 to 0.7/1.0, from a package developer perspective

  • Do I still need a REQUIRE file?

  • Do I need to write Project.toml manually or are there tools?

  • How do I generate a uuid if I manually write a Project.toml?

  • Is there a tool that converts a REQUIRE into a Project.toml for me?

  • What happens if the content of REQUIRE and Project.toml differ?

  • Do I need to move my package from METADATA.jl to the new “General” registry?

  • Do I check in Manifest.toml into my repo?

  • How do I tag a version? Do I use PkgDev.jl or do I use attobot?

Private packages / custom registries

  • How can I install / update / develop a private package / git repo?

  • Can I make my private packages depend on other private packages? What about versions and version constraints?

  • What is the purpose of a registry?

  • What is registry, technically speaking?

  • How can I set up my own registry?

Using latest Pkg.jl

  • How can I use the latest master of Pkg.jl instead of the Pkg package shipped with my Julia installation?

Internals

  • Which environment variables / global variables control the behavior of Pkg?

Troubleshooting

  • Julia says “… no versions found …” - what can I do about it?

出处: https://github.com/JuliaLang/Pkg.jl/issues/615


#5

根据FAQ,大家可以在这里找到所有注册的包的名字,UUID和代码地址


#6

这是其中一个例子

对于这个绘图包

虽然1.0已经可以用了,但是测试目前还有问题

所以我的建议是跟踪你需要的包的更新过程,看看他们的issues,这是一次难得的学习机会。软件都是人写的,不是天下掉下的馅饼。


#7

再接着看这个问题是怎么解决的。上面的issue是两天前的。昨天作者加了更新:

我刚在在自己的机器上测试了一下

Sams-MBP:~ sam$ exec '/Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia'
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.0 (2018-08-08)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/          

julia> 

(v1.0) pkg> add GR

Updating registry at `~/.julia/registries/General`

Updating git-repo `https://github.com/JuliaRegistries/General.git`

 Resolving package versions...

 Installed RecipesBase ── v0.5.0

 Installed PlotUtils ──── v0.5.2

 Installed Contour ────── v0.5.1

 Installed UnicodePlots ─ v0.3.1

 Installed Colors ─────── v0.9.2

Updating `~/.julia/environments/v1.0/Project.toml`

  [91a5bcdd] ~ Plots v0.18.0+ #340cd0a (https://github.com/JuliaPlots/Plots.jl) ⇒ v0.19.1+ #340cd0a (https://github.com/JuliaPlots/Plots.jl)

Updating `~/.julia/environments/v1.0/Manifest.toml`

  [5ae59095] ↑ Colors v0.9.0 ⇒ v0.9.2

  [d38c429a] ↑ Contour v0.5.0 ⇒ v0.5.1

  [995b91a9] ↑ PlotUtils v0.5.1 ⇒ v0.5.2

  [91a5bcdd] ~ Plots v0.18.0+ #340cd0a (https://github.com/JuliaPlots/Plots.jl) ⇒ v0.19.1+ #340cd0a (https://github.com/JuliaPlots/Plots.jl)

  [3cdcf5f2] ↑ RecipesBase v0.4.0 ⇒ v0.5.0

  [b8865327] ↑ UnicodePlots v0.3.0 ⇒ v0.3.1

(v1.0) pkg> test GR

Testing GR

Status `/var/folders/y3/s46lfx1d1yz7dzgbhw25bxnm0000gn/T/tmpGlKFSQ/Manifest.toml`

  [28b8d3ca] GR v0.32.2

  [2a0f44e3] Base64 [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Base64`]

  [8bb1440f] DelimitedFiles [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/DelimitedFiles`]

  [8ba89e20] Distributed [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Distributed`]

  [b77e0a4c] InteractiveUtils [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/InteractiveUtils`]

  [8f399da3] Libdl [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Libdl`]

  [37e2e46d] LinearAlgebra [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/LinearAlgebra`]

  [56ddb016] Logging [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Logging`]

  [d6f4376e] Markdown [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Markdown`]

  [a63ad114] Mmap [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Mmap`]

  [9a3f8284] Random [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Random`]

  [9e88b42a] Serialization [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Serialization`]

  [6462fe0b] Sockets [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Sockets`]

  [8dfed614] Test [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Test`]

running /Users/sam/.julia/packages/GR/crXzk/test/ex.jl ...

running /Users/sam/.julia/packages/GR/crXzk/test/griddata.jl ...

ERROR: LoadError: LoadError: UndefVarError: srand not defined

Stacktrace:

 [1] top-level scope at none:0

 [2] include at ./boot.jl:317 [inlined]

 [3] include_relative(::Module, ::String) at ./loading.jl:1038

 [4] include(::Module, ::String) at ./sysimg.jl:29

 [5] include(::String) at ./client.jl:388

 [6] top-level scope at /Users/sam/.julia/packages/GR/crXzk/test/runtests.jl:20 [inlined]

 [7] top-level scope at ./none:0

 [8] include at ./boot.jl:317 [inlined]

 [9] include_relative(::Module, ::String) at ./loading.jl:1038

 [10] include(::Module, ::String) at ./sysimg.jl:29

 [11] include(::String) at ./client.jl:388

 [12] top-level scope at none:0

in expression starting at /Users/sam/.julia/packages/GR/crXzk/test/griddata.jl:1

in expression starting at /Users/sam/.julia/packages/GR/crXzk/test/runtests.jl:16

ERROR: Package GR errored during testing

还是出错,怎么回事?
原因是我们用add GR安装的还是注册的老版本,修改的版本还没有发布。

我们可以用下面的办法之间安装特定的那个版本

测试通过了:

(v1.0) pkg> add GR#4ee80b8657aff96e3ca4a64d45fd4e7a4b7acc29
  Updating git-repo `https://github.com/jheinen/GR.jl.git`
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
  [28b8d3ca] ~ GR v0.32.2+ #8f4db1d (https://github.com/jheinen/GR.jl.git) ⇒ v0.32.2+ #4ee80b8 (https://github.com/jheinen/GR.jl.git)
  Updating `~/.julia/environments/v1.0/Manifest.toml`
  [28b8d3ca] ~ GR v0.32.2+ #8f4db1d (https://github.com/jheinen/GR.jl.git) ⇒ v0.32.2+ #4ee80b8 (https://github.com/jheinen/GR.jl.git)
  Building GR → `~/.julia/packages/GR/3AqUe/deps/build.log`

(v1.0) pkg> test GR
   Testing GR
    Status `/var/folders/y3/s46lfx1d1yz7dzgbhw25bxnm0000gn/T/tmpbsecWr/Manifest.toml`
  [28b8d3ca] GR v0.32.2+ #4ee80b8 (https://github.com/jheinen/GR.jl.git)
  [2a0f44e3] Base64  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Base64`]
  [8bb1440f] DelimitedFiles  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/DelimitedFiles`]
  [8ba89e20] Distributed  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Distributed`]
  [b77e0a4c] InteractiveUtils  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/InteractiveUtils`]
  [8f399da3] Libdl  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Libdl`]
  [37e2e46d] LinearAlgebra  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/LinearAlgebra`]
  [56ddb016] Logging  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Logging`]
  [d6f4376e] Markdown  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Markdown`]
  [a63ad114] Mmap  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Mmap`]
  [9a3f8284] Random  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Random`]
  [9e88b42a] Serialization  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Serialization`]
  [6462fe0b] Sockets  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Sockets`]
  [8dfed614] Test  [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Test`]
running /Users/sam/.julia/packages/GR/3AqUe/test/ex.jl ...
running /Users/sam/.julia/packages/GR/3AqUe/test/griddata.jl ...
   Testing GR tests passed 

当然,在我写这个回复时,作者正在更新:https://github.com/jheinen/GR.jl/commits/master

欢迎来到实时软件开发时代。


#8

Julia 团队的推荐方式是如果你现在需要应用包和1.0功能,请使用0.7 版本。0.7和1.0的区别是不兼容的地方会警告而不是报错。