Error when using PyPlot and Gtk package

Hi,

I am getting errors when using PyPlot and Gtk together:

julia> using PyPlot, Gtk
ERROR: InitError: could not load library "/Users/hyzhou/.julia/artifacts/c6f53b0adfb48f611ad41b73461d9419117d3503/lib/libfontconfig.1.dylib"
dlopen(/Users/hyzhou/.julia/artifacts/c6f53b0adfb48f611ad41b73461d9419117d3503/lib/libfontconfig.1.dylib, 1): Library not loaded: @rpath/libfreetype.6.dylib
  Referenced from: /Users/hyzhou/.julia/artifacts/c6f53b0adfb48f611ad41b73461d9419117d3503/lib/libfontconfig.1.dylib
  Reason: Incompatible library version: libfontconfig.1.dylib requires version 24.0.0 or later, but libfreetype.6.dylib provides version 23.0.0
Stacktrace:
 [1] #dlopen#3(::Bool, ::typeof(Libdl.dlopen), ::String, ::UInt32) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Libdl/src/Libdl.jl:109
 [2] dlopen at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Libdl/src/Libdl.jl:109 [inlined] (repeats 2 times)
 [3] __init__() at /Users/hyzhou/.julia/packages/Fontconfig_jll/bvmOQ/src/wrappers/x86_64-apple-darwin14.jl:358
 [4] _include_from_serialized(::String, ::Array{Any,1}) at ./loading.jl:692
 [5] _require_search_from_serialized(::Base.PkgId, ::String) at ./loading.jl:776
 [6] _tryrequire_from_serialized(::Base.PkgId, ::UInt64, ::String) at ./loading.jl:707
 [7] _require_search_from_serialized(::Base.PkgId, ::String) at ./loading.jl:765
 [8] _require(::Base.PkgId) at ./loading.jl:1001
 [9] require(::Base.PkgId) at ./loading.jl:922
 [10] require(::Module, ::Symbol) at ./loading.jl:917
during initialization of module Fontconfig_jll

However, if I switch the import order,

julia> using Gtk, PyPlot
[ Info: Installing pyqt package to avoid buggy tkagg backend.
┌ Warning: No working GUI backend found for matplotlib
└ @ PyPlot ~/.julia/packages/PyPlot/4wzW1/src/init.jl:165
ERROR: InitError: PyError (PyImport_ImportModule

The Python package matplotlib.pyplot could not be found by pyimport. Usually this means
that you did not install matplotlib.pyplot in the Python version being used by PyCall.

PyCall is currently configured to use the Julia-specific Python distribution
installed by the Conda.jl package.  To install the matplotlib.pyplot module, you can
use `pyimport_conda("matplotlib.pyplot", PKG)`, where PKG is the Anaconda
package the contains the module matplotlib.pyplot, or alternatively you can use the
Conda package directly (via `using Conda` followed by `Conda.add` etcetera).

Alternatively, if you want to use a different Python distribution on your
system, such as a system-wide Python (as opposed to the Julia-specific Python),
you can re-configure PyCall with that Python.   As explained in the PyCall
documentation, set ENV["PYTHON"] to the path/name of the python executable
you want to use, run Pkg.build("PyCall"), and re-launch Julia.

) <class 'ImportError'>
ImportError('dlopen(/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/_png.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libpng16.16.dylib\n  Referenced from: /Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/_png.cpython-37m-darwin.so\n  Reason: Incompatible library version: _png.cpython-37m-darwin.so requires version 52.0.0 or later, but libpng16.16.37.0.dylib provides version 16.0.0')
  File "/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/colorbar.py", line 32, in <module>
    import matplotlib.contour as contour
  File "/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/contour.py", line 19, in <module>
    import matplotlib.text as text
  File "/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/text.py", line 18, in <module>
    from .textpath import TextPath  # Unused, but imported by others.
  File "/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/textpath.py", line 12, in <module>
    from matplotlib.mathtext import MathTextParser
  File "/Users/hyzhou/.julia/conda/3/lib/python3.7/site-packages/matplotlib/mathtext.py", line 34, in <module>
    from matplotlib import _png, cbook, colors as mcolors, get_data_path, rcParams

Stacktrace:
 [1] pyimport(::String) at /Users/hyzhou/.julia/packages/PyCall/kAhnQ/src/PyCall.jl:536
 [2] __init__() at /Users/hyzhou/.julia/packages/PyPlot/4wzW1/src/init.jl:195
 [3] _include_from_serialized(::String, ::Array{Any,1}) at ./loading.jl:692
 [4] _require_search_from_serialized(::Base.PkgId, ::String) at ./loading.jl:776
 [5] _require(::Base.PkgId) at ./loading.jl:1001
 [6] require(::Base.PkgId) at ./loading.jl:922
 [7] require(::Module, ::Symbol) at ./loading.jl:917
during initialization of module PyPlot

I am wondering what is causing this behind the scene.

Did you find a solution? I just started having this problem today. All was well a couple months ago. Now when I try “using module_that_needs_matplotlib” I get this …

Info: Installing matplotlib via the Conda matplotlib package…
[ Info: Running conda install -y matplotlib in root environment
Collecting package metadata (current_repodata.json): done
Solving environment: done
#All requested packages already installed.

Followed by this

ERROR: LoadError: InitError: PyError (PyImport_ImportModule
The Python package matplotlib could not be found by pyimport. Usually this means
that you did not install matplotlib in the Python version being used by PyCall.

I don’t seem to be the only one having this problem, but am having trouble understanding the solutions.

Please see this issue. I didn’t continue from that point on, but changing the PyPlot backend should work:
ENV["MPLBACKEND"]="tkagg"

Thanks. how do I do that?

I tried that, but no joy. Now I get a different error

[ Info: Running conda install -y matplotlib in root environment

>>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

Traceback (most recent call last):
  File "/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/conda/exceptions.py", line 1079, in __call__
    return func(*args, **kwargs)
  File "/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/conda/cli/main.py", line 84, in _main
    exit_code = do_call(args, p)
  File "/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/conda/cli/conda_argparse.py", line 82, in do_call
    return getattr(module, func_name)(args, parser)
  File "/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/conda/cli/main_install.py", line 20, in execute
    install(args, parser, 'install')
  File "/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/conda/cli/install.py", line 116, in install
    if context.use_only_tar_bz2:
  File "/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/conda/base/context.py", line 710, in use_only_tar_bz2
    import conda_package_handling.api
ModuleNotFoundError: No module named 'conda_package_handling.api'

Something seems badly broken.

Spooky. There was a post in a related thread suggesting to

rm -rf .julia

and start over. That seemed to work for me. I have no idea why.