# Pyplot and conda problems

**URL:** <https://discourse.julialang.org/t/pyplot-and-conda-problems/59229>\
**Category:** General Usage\
**Created:** [April 13, 2021, 6:56pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229 "2021-04-13T18:56:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 13, 2021, 6:56pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/1 "2021-04-13T18:56:55Z")

</div>

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

```julia
using Plots
pyplot()

```

now generate the error:

```julia
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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 13, 2021, 7:13pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/2 "2021-04-13T19:13:27Z")

</div>

Experienced the same problem in Win10 and solved it thanks to the magic powers of @stevengj, see his solution in this [post](https://discourse.julialang.org/t/dreaded-pycall-build-error/56761/6).

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

```nohighlight
pip install --user matplotlib

```

Then do in Julia:

```julia
Pkg.build("PyCall")

```

Et voilà:

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 13, 2021, 7:16pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/3 "2021-04-13T19:16:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 13, 2021, 7:17pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/4 "2021-04-13T19:17:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 13, 2021, 7:33pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/5 "2021-04-13T19:33:30Z")

</div>

Tried that. Got:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 13, 2021, 7:36pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/6 "2021-04-13T19:36:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 13, 2021, 8:04pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/7 "2021-04-13T20:04:40Z")

</div>

This could be related to [https://github.com/jheinen/GR.jl/issues/392](https://github.com/jheinen/GR.jl/issues/392)

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 13, 2021, 8:11pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/8 "2021-04-13T20:11:54Z")

</div>

Same error

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 13, 2021, 8:12pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/9 "2021-04-13T20:12:43Z")

</div>

> [@gideonsimpson](#):
>
> ```julia
> 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')
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 13, 2021, 8:35pm UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/10 "2021-04-13T20:35:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [April 14, 2021, 1:23am UTC](https://discourse.julialang.org/t/pyplot-and-conda-problems/59229/11 "2021-04-14T01:23:02Z")

</div>

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