How to install a package in Julia 1.0 (UndefVarError: Pkg not defined)

狂风中的少年 提交于 2019-12-01 14:59:20

In Julia 1.0, there are two ways to install a package. First, you can do

using Pkg
Pkg.add("Packagename")

or you can use the Pkg REPL mode by pressing ] (similar to ?, help mode, and ;, shell mode):

(v1.0) pkg> add Packagename

You can find more information here: https://docs.julialang.org/en/stable/stdlib/Pkg/# and here (live demo): https://youtu.be/GBi__3nF-rM?t=28m1s

Julia 1.0 is brand new. It has been released a few days ago, so some packages aren't yet compatible with 1.0. Sometimes it helps to install the master branch of a package instead of the last tagged release. On my machine I had to do

(v1.0) pkg> add LaTeXStrings#master
(v1.0) pkg> add PyPlot

to get PyPlot to work. Hope this helps.

UPDATE: LaTeXStrings has been updated (tagged). The first line above is therefore not necessary anymore.

UPDATE2: Another (shorter) live demo can be found here: https://www.youtube.com/watch?v=76KL8aSz0Sg

UPDATE3: There is a third way of interacting with the package manager, namely "pkg strings":

using Pkg
pkg"add Packagename"

As someone new to Julia, my initial confusion on the Julia 1.0 package manager was directly related to the OP's problem. I have not used pre-Julia 1.0 REPL's, but it would appear that it used to be that Pkg.add("Example") worked right out of the box--no using Pkg was required. There are examples floating around the web of this being the way to get started. The new Julia user, like me, might not initially be aware of this change.

However, the Julia 1.0 REPL requires the user to explicitly state using Pkg before employing the Pkg.add.("Example") command.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!