続:Python, JupyterLab, Julia, IJulia on Anaconda のインストールとmatplotlibの消失、からの再メモインストールと格闘、そして次回に続く

juliaを入れるとmatplotlibが動かなくなる!!

 いやぁ、マジでわけわからん。Anacondaインストール(exeファイルから)、Juliaインストール(exeファイルから)、PyPlotインストール(Pkg.addから)で動かなくなってしまう…なんか根本的に勘違いしてるのかなぁ…ググっても知識不足のせいで答えにたどり着かないよ…

 過去の記事はこちら
easygoingrider.hatenablog.com
easygoingrider.hatenablog.com

 うまくいった記事はこちら(つまり、現在のページでは何も解決していません)
easygoingrider.hatenablog.com

以下、何もうまくいっていないので、自分が見つけた解決方法は上の記事をご覧ください。
Juliaを入れたらmatplotlibが動かなくなってしまう。とりあえずjulia, anacondaをアンインストールしてanacondaを再インストール。
使っているanacondaは、「Anaconda3-2021.05-Windows-x86_64.exe」。また、この後のjuliaのインストールに使用したのは「julia-1.6.3-win64.exe」(と時々「julia-1.0.5-win64.exe」)
まずはanacondaをexeファイルからインストールした状態でmatplotが動くかどうかを確認。
まずは何もせずに

conda list | find "matplot"
matplotlib                3.3.4            py38haa95532_0
matplotlib-base           3.3.4            py38h49ac443_0

どうやらpythonのバージョンは3.8.8の模様。今度は動いた。

(base) C:\Users\user>python
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x0000020DCFF0E160>]
>>> plt.show()

また、jupyterlabが入っているかどうかチェック

conda list | find "jupyter"
jupyter                   1.0.0                    py38_7
jupyter-packaging         0.7.12             pyhd3eb1b0_0
jupyter_client            6.1.12             pyhd3eb1b0_0
jupyter_console           6.4.0              pyhd3eb1b0_0
jupyter_core              4.7.1            py38haa95532_0
jupyter_server            1.4.1            py38haa95532_0
jupyterlab                3.0.14             pyhd3eb1b0_1
jupyterlab_pygments       0.1.2                      py_0
jupyterlab_server         2.4.0              pyhd3eb1b0_0
jupyterlab_widgets        1.0.0              pyhd3eb1b0_1

入っとるがな。
jupyterlabを起動。あれ、動いた。つまりわざわざインストールしなくてもパッケージに含まれてるのか。しかもjupyterlabを起動したら、最初のカーネル選択画面にアンインストールしたはずのjuliaも入ってる…?あ、でもクリックしたらちゃんと起動しなかった。

f:id:easygoingrider:20211103012350p:plain
カーネルエラー!!

とポップアップ。
とりあえず気を取り直してjuliaをexeファイルからインストール、さらにpyplotをインストール
pkgを起動してadd PyPlotを実行

       Added registry `General` to `C:\Users\user\.julia\registries\General`
   Resolving package versions...
   Installed LaTeXStrings ────── v1.2.1
   Installed PyPlot ──────────── v2.10.0
   Installed Parsers ─────────── v2.1.1
   Installed FixedPointNumbers ─ v0.8.4
   Installed ColorTypes ──────── v0.11.0
   Installed MacroTools ──────── v0.5.9
   Installed JSON ────────────── v0.21.2
   Installed Colors ──────────── v0.12.8
   Installed Reexport ────────── v1.2.2
   Installed VersionParsing ──── v1.2.1
   Installed Conda ───────────── v1.5.2
   Installed PyCall ──────────── v1.92.5
    Updating `C:\Users\user\.julia\environments\v1.6\Project.toml`
  [d330b81b] + PyPlot v2.10.0
  [3da002f7] + ColorTypes v0.11.0
  [8f4d0f93] + Conda v1.5.2
  [53c48c17] + FixedPointNumbers v0.8.4
  [682c06a0] + JSON v0.21.2
  [b964fa9f] + LaTeXStrings v1.2.1
  [1914dd2f] + MacroTools v0.5.9
  [69de0a69] + Parsers v2.1.1
  [438e738f] + PyCall v1.92.5
  [d330b81b] + PyPlot v2.10.0
  [189a3867] + Reexport v1.2.2
  [81def892] + VersionParsing v1.2.1
  [2a0f44e3] + Base64
  [ade2ca70] + Dates
  [b77e0a4c] + InteractiveUtils
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [a63ad114] + Mmap
  [de0858da] + Printf
  [9a3f8284] + Random
  [9e88b42a] + Serialization
  [6462fe0b] + Sockets
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [8dfed614] + Test
  [4ec0a83e] + Unicode
    Building Conda ─→ `C:\Users\user\.julia\scratchspaces\44cfe95a-1eb2-52ea-b672-e2afdf69b78f\299304989a5e6473d985212c28928899c74e9421\build.log`
    Building PyCall → `C:\Users\user\.julia\scratchspaces\44cfe95a-1eb2-52ea-b672-e2afdf69b78f\4ba3651d33ef76e24fef6a598b63ffd1c5e1cd17\build.log`
Precompiling project...
  8 dependencies successfully precompiled in 22 seconds (4 already precompiled)

なんかいろいろちゃんと入った。さらにPyCall も勝手に入った。
次に、

using PyPlot

としたら、なんかインストールが勝手に始まってしまった。
画面が流れてしまって何が入ったのかをメモできなかったが、勝手にいろいろ入った。
このタイミングでplotしたら落ちた。

julia> using PyPlot
julia> plot([1,2,3])
# このタイミングでjuliaコンソールが消えていく…

wow.
さらにanaconda からpythonを起動、matplotlibを動かしたら落ちるようになった。

(base) C:\Users\user>python
Python 3.9.5 (default, May 18 2021, 14:42:02) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x000002856D6A6970>]
>>> plt.show()

おいおい…
また、なぜかpythonが3.9.5になってる。

以上より、たぶんusing PyPlotもしくは、add PyPlotがあかん。
add PyPlotのタイミングでpythonのバージョンとかどうなってるか調べたい?

とりあえずpythonを3.8.8 に戻してみる。

conda install python=3.8.8

さらにanacondaから描画を試してみる。

(base) C:\Users\user>python
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x0000023E6442A730>]
>>> plt.show()

あかん、グラフでない。
matplotのバージョンもちょっと上がってた。

(base) C:\Users\user>conda list | find "matplot"
matplotlib                3.4.3            py38haa95532_0
matplotlib-base           3.4.3            py38h49ac443_0

一応戻してみる。

conda install matplotlib=3.3.4

戻った。

(base) C:\Users\user>conda list | find "matplot"
matplotlib                3.3.4            py38haa95532_0
matplotlib-base           3.3.4            py38h49ac443_0

でもやっぱりだめ。動かない。ただ、一瞬見えるウィンドウは古臭くなった。上にメニューがある感じ。

(base) C:\Users\user>python
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x000001EBA910CD90>]
>>> plt.show()

ダメや…もう疲れたよ…

再度すべてアンインストールしたのち、再インストール。

まずはanaconda を入れる。
pythonを起動すると3.8.8、matplotlibも動く。
次にpythonをアップデート。だた、3.8.12までにしかならない。
さらにupdate --allがリコメンドされたので、conda update --allを実行。
ここでmatplotlibが動かなくなった。
そもそも今回インストールしたAnaconda3-2021.5はpython 3.8.8までなのか??
qiita.com

つまり、問題はjulaでpyplotを使おうとしたときに、勝手にいろいろアップデートされてしまい、pythonでmatplotlibが動かなくなってしまうこと。というか、これ、PCが古いからとかそういう問題だったりするのか…?

再度アンインストール

まずはAnacondaをインストール。この状態でpython をアップデートしてもバージョンは変わらず3.8.8、matplotlibも正常。
次にjuliaをインストール、今度はJuliaの公式ページを覗きに行った。インストール時にENV["PYTHON"]="" を実行してからPyPlotをaddしとろのことなので、

julia> ENV["PYTHON"]=""
""
julia> using Pkg
julia> Pkg.add("PyPlot")
    Updating registry at `C:\Users\user\.julia\registries\General`
    Updating git-repo `https://github.com/JuliaRegistries/General.git`
   Resolving package versions...
  No Changes to `C:\Users\user\.julia\environments\v1.6\Project.toml`
  No Changes to `C:\Users\user\.julia\environments\v1.6\Manifest.toml`

何も変わってない??とりあえず入ったパッケージを確認。

(@v1.6) pkg> status
      Status `C:\Users\user\.julia\environments\v1.6\Project.toml`
  [d330b81b] PyPlot v2.10.0

その後、anacondaからpythonを実行、グラフがプロットできることを確認。この時点ではまだ問題は発生していない模様。
ここでJulia で using PyPlotを実行、やっぱりエラーが出た。どうやらここでpython3.9になってたっぽい。てかエラーが出るようになっている!?

ERROR: InitError: could not load library "C:\Users\user\.julia\conda\3\python39.dll"
The specified module could not be found.
Stacktrace:
  [1] dlopen(s::String, flags::UInt32; throw_error::Bool)
    @ Base.Libc.Libdl .\libdl.jl:114
  [2] dlopen
    @ .\libdl.jl:114 [inlined]
  [3] __init__()
    @ PyCall C:\Users\user\.julia\packages\PyCall\3fwVL\src\pyinit.jl:149
  [4] _include_from_serialized(path::String, depmods::Vector{Any})
    @ Base .\loading.jl:696
  [5] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String)
    @ Base .\loading.jl:782
  [6] _tryrequire_from_serialized(modkey::Base.PkgId, build_id::UInt64, modpath::String)
    @ Base .\loading.jl:711
  [7] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String)
    @ Base .\loading.jl:771
  [8] _require(pkg::Base.PkgId)
    @ Base .\loading.jl:1020
  [9] require(uuidkey::Base.PkgId)
    @ Base .\loading.jl:936
 [10] require(into::Module, mod::Symbol)
    @ Base .\loading.jl:923
during initialization of module PyCall

うーむ、python39.dllなんてないぞ…てかpythonのバージョン3.8だぞ…
そこで、Julia のバージョンを落としてみる。とりあえず、LTSの1.0.5をインストール、ENV["PYTHON"]=""を実行してから add PyPlot をしてみる。今度はなんかいろいろ入った。

julia> ENV["PYTHON"]=""
""
(v1.0) pkg> add PyPlot
  Updating registry at `C:\Users\user\.julia\registries\General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `C:\Users\user\.julia\environments\v1.0\Project.toml`
  [d330b81b] + PyPlot v2.10.0
  Updating `C:\Users\user\.julia\environments\v1.0\Manifest.toml`
  [3da002f7] + ColorTypes v0.11.0
  [5ae59095] + Colors v0.12.8
  [8f4d0f93] + Conda v1.5.2
  [53c48c17] + FixedPointNumbers v0.8.4
  [682c06a0] + JSON v0.21.2
  [b964fa9f] + LaTeXStrings v1.2.1
  [1914dd2f] + MacroTools v0.5.9
  [69de0a69] + Parsers v2.1.1
  [438e738f] + PyCall v1.92.5
  [d330b81b] + PyPlot v2.10.0
  [189a3867] + Reexport v1.2.2
  [81def892] + VersionParsing v1.2.1
  [2a0f44e3] + Base64
  [ade2ca70] + Dates
  [8ba89e20] + Distributed
  [b77e0a4c] + InteractiveUtils
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [a63ad114] + Mmap
  [de0858da] + Printf
  [9a3f8284] + Random
  [9e88b42a] + Serialization
  [6462fe0b] + Sockets
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [8dfed614] + Test
  [4ec0a83e] + Unicode

pythonやmatplotlibは触られてないっぽいし、期待できるか!?この状態でanacondaからpython, matplotlibを動かしてもちゃんと動いている。
そこで、juliaでusing PyPlotをしてみる!!!!どうや!!!?ダメでしたorg...

julia> using PyPlot
[ Info: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]
ERROR: LoadError: LoadError: could not load library "C:\Users\user\.julia\conda\3\python39.dll"
The specified module could not be found.
. Please run `Pkg.build("PyCall")` if your Python build has changed
Stacktrace:
 [1] error(::String, ::String) at .\error.jl:42
 [2] top-level scope at C:\Users\user\.julia\packages\PyCall\3fwVL\src\startup.jl:51 [inlined]
 [3] top-level scope at .\none:0
 [4] include at .\boot.jl:317 [inlined]
 [5] include_relative(::Module, ::String) at .\loading.jl:1044
 [6] include at .\sysimg.jl:29 [inlined]
 [7] include(::String) at C:\Users\user\.julia\packages\PyCall\3fwVL\src\PyCall.jl:1
 [8] top-level scope at none:0
 [9] include at .\boot.jl:317 [inlined]
 [10] include_relative(::Module, ::String) at .\loading.jl:1044
 [11] include(::Module, ::String) at .\sysimg.jl:29
 [12] top-level scope at none:2
 [13] eval at .\boot.jl:319 [inlined]
 [14] eval(::Expr) at .\client.jl:393
 [15] top-level scope at .\none:3
in expression starting at C:\Users\user\.julia\packages\PyCall\3fwVL\src\startup.jl:41
in expression starting at C:\Users\user\.julia\packages\PyCall\3fwVL\src\PyCall.jl:38
ERROR: LoadError: Failed to precompile PyCall [438e738f-606a-5dbb-bf0a-cddfbfd45ab0] to C:\Users\user\.julia\compiled\v1.0\PyCall\GkzkC.ji.
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] compilecache(::Base.PkgId, ::String) at .\loading.jl:1203
 [3] _require(::Base.PkgId) at .\loading.jl:960
 [4] require(::Base.PkgId) at .\loading.jl:858
 [5] require(::Module, ::Symbol) at .\loading.jl:853
 [6] include at .\boot.jl:317 [inlined]
 [7] include_relative(::Module, ::String) at .\loading.jl:1044
 [8] include(::Module, ::String) at .\sysimg.jl:29
 [9] top-level scope at none:2
 [10] eval at .\boot.jl:319 [inlined]
 [11] eval(::Expr) at .\client.jl:393
 [12] top-level scope at .\none:3
in expression starting at C:\Users\user\.julia\packages\PyPlot\XaELc\src\PyPlot.jl:4
ERROR: Failed to precompile PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee] to C:\Users\user\.julia\compiled\v1.0\PyPlot\oatAj.ji.
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] compilecache(::Base.PkgId, ::String) at .\loading.jl:1203
 [3] _require(::Base.PkgId) at .\loading.jl:960
 [4] require(::Base.PkgId) at .\loading.jl:858
 [5] require(::Module, ::Symbol) at .\loading.jl:853

PyCallをビルドしろ、か…
なぜかここでビルドせずに、rm PyPlotでPyPlotを削除、バージョンを2.9.0へ下げてみる(迷走中)。

(v1.0) pkg> add PyPlot@2.9.0
 Resolving package versions...
 Installed PyPlot ─ v2.9.0
  Updating `C:\Users\user\.julia\environments\v1.0\Project.toml`
  [d330b81b] + PyPlot v2.9.0
  Updating `C:\Users\user\.julia\environments\v1.0\Manifest.toml`
  [3da002f7] + ColorTypes v0.11.0
  [5ae59095] + Colors v0.12.8
  [8f4d0f93] + Conda v1.5.2
  [53c48c17] + FixedPointNumbers v0.8.4
  [682c06a0] + JSON v0.21.2
  [b964fa9f] + LaTeXStrings v1.2.1
  [1914dd2f] + MacroTools v0.5.9
  [69de0a69] + Parsers v2.1.1
  [438e738f] + PyCall v1.92.5
  [d330b81b] + PyPlot v2.9.0
  [189a3867] + Reexport v1.2.2
  [81def892] + VersionParsing v1.2.1
  [2a0f44e3] + Base64
  [ade2ca70] + Dates
  [8ba89e20] + Distributed
  [b77e0a4c] + InteractiveUtils
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [a63ad114] + Mmap
  [de0858da] + Printf
  [9a3f8284] + Random
  [9e88b42a] + Serialization
  [6462fe0b] + Sockets
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [8dfed614] + Test
  [4ec0a83e] + Unicode

この時点でもpythonでmatplotlibに問題なし。ちゃんとグラフ描ける。
もっかいusing PyPlotしてみる。

julia> using PyPlot
[ Info: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]
ERROR: LoadError: LoadError: could not load library "C:\Users\user\.julia\conda\3\python39.dll"
The specified module could not be found.
. Please run `Pkg.build("PyCall")` if your Python build has changed
Stacktrace:
 [1] error(::String, ::String) at .\error.jl:42
 [2] top-level scope at C:\Users\user\.julia\packages\PyCall\3fwVL\src\startup.jl:51 [inlined]
 [3] top-level scope at .\none:0
 [4] include at .\boot.jl:317 [inlined]
 [5] include_relative(::Module, ::String) at .\loading.jl:1044
 [6] include at .\sysimg.jl:29 [inlined]
 [7] include(::String) at C:\Users\user\.julia\packages\PyCall\3fwVL\src\PyCall.jl:1
 [8] top-level scope at none:0
 [9] include at .\boot.jl:317 [inlined]
 [10] include_relative(::Module, ::String) at .\loading.jl:1044
 [11] include(::Module, ::String) at .\sysimg.jl:29
 [12] top-level scope at none:2
 [13] eval at .\boot.jl:319 [inlined]
 [14] eval(::Expr) at .\client.jl:393
 [15] top-level scope at .\none:3
in expression starting at C:\Users\user\.julia\packages\PyCall\3fwVL\src\startup.jl:41
in expression starting at C:\Users\user\.julia\packages\PyCall\3fwVL\src\PyCall.jl:38
ERROR: LoadError: Failed to precompile PyCall [438e738f-606a-5dbb-bf0a-cddfbfd45ab0] to C:\Users\user\.julia\compiled\v1.0\PyCall\GkzkC.ji.
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] compilecache(::Base.PkgId, ::String) at .\loading.jl:1203
 [3] _require(::Base.PkgId) at .\loading.jl:960
 [4] require(::Base.PkgId) at .\loading.jl:858
 [5] require(::Module, ::Symbol) at .\loading.jl:853
 [6] include at .\boot.jl:317 [inlined]
 [7] include_relative(::Module, ::String) at .\loading.jl:1044
 [8] include(::Module, ::String) at .\sysimg.jl:29
 [9] top-level scope at none:2
 [10] eval at .\boot.jl:319 [inlined]
 [11] eval(::Expr) at .\client.jl:393
 [12] top-level scope at .\none:3
in expression starting at C:\Users\user\.julia\packages\PyPlot\XHEG0\src\PyPlot.jl:4
ERROR: Failed to precompile PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee] to C:\Users\user\.julia\compiled\v1.0\PyPlot\oatAj.ji.
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] compilecache(::Base.PkgId, ::String) at .\loading.jl:1203
 [3] _require(::Base.PkgId) at .\loading.jl:960
 [4] require(::Base.PkgId) at .\loading.jl:858
 [5] require(::Module, ::Symbol) at .\loading.jl:853

あかん、ダメや。python39.dllを要求される。実際フォルダ(C:\Users\user\.julia\conda\3\)を覗くと、python3.dll, python38.dllしかない。PyPlotv2.8.2でも同様。これ、pyplotのせいではない??
エラーコードにしたがって、ビルドしてみる。

julia> using Pkg
julia> Pkg.build("PyCall")
  Building Conda ─→ `C:\Users\user\.julia\packages\Conda\sNGum\deps\build.log`
  Building PyCall → `C:\Users\user\.julia\packages\PyCall\3fwVL\deps\build.log`

再度using Pyplot を実行。

julia> using PyPlot
[ Info: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]

お、動いた!!がしかし、
plot([1,2,3], [4,5,6])
でjuliaごと落ちてしまった…
もういやや!!
次回へ続く…