Pyplot and conda problems

Something seems to have gone wrong with my conda/julia setup. The following commands:

using Plots
pyplot()

now generate the error:

InitError: PyError (PyImport_ImportModule

The Python package matplotlib.pyplot could not be imported 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/gideonsimpson/.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/gideonsimpson/.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 51.0.0 or later, but libpng16.16.37.0.dylib provides version 16.0.0')
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 31, in <module>
    import matplotlib.colorbar
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/contour.py", line 21, in <module>
    import matplotlib.text as text
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/text.py", line 22, in <module>
    from .textpath import TextPath  # Unused, but imported by others.
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/textpath.py", line 20, in <module>
    from matplotlib.mathtext import MathTextParser
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/mathtext.py", line 38, in <module>
    from matplotlib import _png, colors as mcolors, get_data_path, rcParams

during initialization of module PyPlot

Stacktrace:
  [1] pyimport(name::String)
    @ PyCall ~/.julia/packages/PyCall/BD546/src/PyCall.jl:550
  [2] __init__()
    @ PyPlot ~/.julia/packages/PyPlot/XHEG0/src/init.jl:195
  [3] _include_from_serialized(path::String, depmods::Vector{Any})
    @ Base ./loading.jl:674
  [4] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String)
    @ Base ./loading.jl:760
  [5] _require(pkg::Base.PkgId)
    @ Base ./loading.jl:998
  [6] require(uuidkey::Base.PkgId)
    @ Base ./loading.jl:914
  [7] require(into::Module, mod::Symbol)
    @ Base ./loading.jl:901
  [8] top-level scope
    @ ~/.julia/packages/Plots/SVksJ/src/backends.jl:505
  [9] eval
    @ ./boot.jl:360 [inlined]
 [10] _initialize_backend(#unused#::Plots.PyPlotBackend)
    @ Plots ~/.julia/packages/Plots/SVksJ/src/backends.jl:504
 [11] backend
    @ ~/.julia/packages/Plots/SVksJ/src/backends.jl:174 [inlined]
 [12] pyplot(; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Plots ~/.julia/packages/Plots/SVksJ/src/backends.jl:31
 [13] pyplot()
    @ Plots ~/.julia/packages/Plots/SVksJ/src/backends.jl:31
 [14] top-level scope
    @ In[2]:1
 [15] eval
    @ ./boot.jl:360 [inlined]
 [16] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1094

I have tried removing my conda installation and reinstalling with the same packages, but I keep getting these errors.

Experienced the same problem in Win10 and solved it thanks to the magic powers of @stevengj, see his solution in this post.

After the Miniforge and the Python 3.8 installation, open a Win10 terminal and type command to install matplotlib:

pip install --user matplotlib

Then do in Julia:

Pkg.build("PyCall")

Et voilà:

I’ve tried rebuilding. Is the issue that I need to abandon the conda installation?

No clue about the issue, just tried to follow @stevengj’s advice, used Miniforge as explained and it worked.

Tried that. Got:

julia> ENV["CONDA_JL_USE_MINIFORGE"] = "1"
"1"

(@v1.6) pkg> build PyCall
    Building Conda ─→ `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/6231e40619c15148bcb80aa19d731e629877d762/build.log`
    Building PyCall → `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/169bb8ea6b1b143c5cf57df6d34d022a7b60c6db/build.log`

julia> using Plots

julia> pyplot()
[ Info: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]
[ Info: Installing pyqt package to avoid buggy tkagg backend.
┌ Warning: No working GUI backend found for matplotlib
└ @ PyPlot ~/.julia/packages/PyPlot/XHEG0/src/init.jl:165
sys:1: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'Qt5Agg' by the following code:
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/backends/__init__.py", line 16, in <module>
    line for line in traceback.format_stack()


ERROR: InitError: PyError (PyImport_ImportModule

The Python package matplotlib.pyplot could not be imported 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/gideonsimpson/.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/gideonsimpson/.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 51.0.0 or later, but libpng16.16.37.0.dylib provides version 16.0.0')
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 31, in <module>
    import matplotlib.colorbar
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/contour.py", line 21, in <module>
    import matplotlib.text as text
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/text.py", line 22, in <module>
    from .textpath import TextPath  # Unused, but imported by others.
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/textpath.py", line 20, in <module>
    from matplotlib.mathtext import MathTextParser
  File "/Users/gideonsimpson/.julia/conda/3/lib/python3.7/site-packages/matplotlib/mathtext.py", line 38, in <module>
    from matplotlib import _png, colors as mcolors, get_data_path, rcParams

Stacktrace:
  [1] pyimport(name::String)
    @ PyCall ~/.julia/packages/PyCall/BD546/src/PyCall.jl:550
  [2] __init__()
    @ PyPlot ~/.julia/packages/PyPlot/XHEG0/src/init.jl:195
  [3] _include_from_serialized(path::String, depmods::Vector{Any})
    @ Base ./loading.jl:674
  [4] _require_from_serialized(path::String)
    @ Base ./loading.jl:727
  [5] _require(pkg::Base.PkgId)
    @ Base ./loading.jl:1031
  [6] require(uuidkey::Base.PkgId)
    @ Base ./loading.jl:914
  [7] require(into::Module, mod::Symbol)
    @ Base ./loading.jl:901
  [8] top-level scope
    @ ~/.julia/packages/Plots/SVksJ/src/backends.jl:505
  [9] eval
    @ ./boot.jl:360 [inlined]
 [10] _initialize_backend(#unused#::Plots.PyPlotBackend)
    @ Plots ~/.julia/packages/Plots/SVksJ/src/backends.jl:504
 [11] backend
    @ ~/.julia/packages/Plots/SVksJ/src/backends.jl:174 [inlined]
 [12] pyplot(; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Plots ~/.julia/packages/Plots/SVksJ/src/backends.jl:31
 [13] pyplot()
    @ Plots ~/.julia/packages/Plots/SVksJ/src/backends.jl:31
 [14] top-level scope
    @ REPL[4]:1
during initialization of module PyPlot

Did you skip the step: “…setting ENV["PYTHON"] to the path of python.exe before building PyCall”?

This could be related to Qt library conflict with v0.57.3 · Issue #392 · jheinen/GR.jl · GitHub

Same error

This is the underlying error: a library conflict between libpng versions. “DLL hell” is no fun. Usually fixed by updating something so that the library versions are compatible.

Ugh. I blew away my miniconda installation and tried to avoid any custom channels, and that’s what I still got.

Ok, I fixed it by cloning the exact versions/builds from a working conda installation on another machine :expressionless: