PyPlot: The Python package matplotlib.pyplot could not be found by pyimport

Hi,
I have installed Julia 0.6.2 in my Ubuntu 16.04 and is using Conda for julia’s python distribution.

julia> versioninfo()
Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-4570S CPU @ 2.90GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

I have Pkg.add('PyPlot') to my installation, but it gives wierd error when I try to use PyPlot:

julia> using PyPlot
INFO: Recompiling stale cache file /home/vish/.julia/lib/v0.6/PyCall.ji for module PyCall.
INFO: Recompiling stale cache file /home/vish/.julia/lib/v0.6/PyPlot.ji for module PyPlot.
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 u'Qt5Agg' by the following code:
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
    line for line in traceback.format_stack()


WARNING: No working GUI backend found for matplotlib
ERROR: InitError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

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.

) <type 'exceptions.ImportError'>
ImportError("/lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/../../.././libpng16.so.16)",)
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/pyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
    import matplotlib.font_manager as font_manager
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
    from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir

Stacktrace:
 [1] pyerr_check at /home/vish/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]
 [2] pyerr_check at /home/vish/.julia/v0.6/PyCall/src/exception.jl:61 [inlined]
 [3] macro expansion at /home/vish/.julia/v0.6/PyCall/src/exception.jl:81 [inlined]
 [4] pyimport(::String) at /home/vish/.julia/v0.6/PyCall/src/PyCall.jl:374
 [5] __init__() at /home/vish/.julia/v0.6/PyPlot/src/init.jl:184
 [6] _include_from_serialized(::String) at ./loading.jl:157
 [7] _require_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at ./loading.jl:200
 [8] _require(::Symbol) at ./loading.jl:464
 [9] require(::Symbol) at ./loading.jl:405
during initialization of module PyPlot

However, my Conda installation shows matplotlib as installed

julia> using Conda

julia> Conda.list()
# packages in environment at /home/vish/.julia/v0.6/Conda/deps/usr:
#
asn1crypto                0.23.0           py27h7b0c810_0  
backports                 1.0              py27h63c9359_1  
backports.functools_lru_cache 1.4              py27he8db605_1  
backports_abc             0.5              py27h7b3c97b_0  
ca-certificates           2017.08.26           h1d4fec5_0  
certifi                   2017.11.5        py27h71e7faf_0  
cffi                      1.11.2           py27ha7929c6_0  
chardet                   3.0.4            py27hfa10054_1  
conda                     4.4.4                    py27_0  
conda-env                 2.6.0                h36134e3_1  
cryptography              2.1.4            py27h6697b16_0  
cycler                    0.10.0           py27hc7354d3_0  
dbus                      1.10.22              h3b5a359_0  
enum34                    1.1.6            py27h99a27e9_1  
expat                     2.2.5                he0dffb1_0  
fontconfig                2.12.4               h88586e7_1  
freetype                  2.8                  hab7d2ae_1  
functools32               3.2.3.2          py27h4ead58f_1  
futures                   3.2.0            py27h7b459c0_0  
glib                      2.53.6               h5d9569c_2  
gst-plugins-base          1.12.2               he3457e5_0  
gstreamer                 1.12.2               h4f93127_0  
icu                       58.2                 h9c2bf20_1  
idna                      2.6              py27h5722d68_1  
intel-openmp              2018.0.0             hc7b2577_8  
ipaddress                 1.0.18           py27h337fd85_0  
jpeg                      9b                   h024ee3a_2  
libedit                   3.1                  heed3624_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 7.2.0                h7cc24e2_2  
libpng                    1.6.32               hbd3595f_4  
libstdcxx-ng              7.2.0                h7a57d05_2  
libxcb                    1.12                 hcd93eb1_4  
libxml2                   2.9.4                h2e8b1d7_6  
matplotlib                2.1.1            py27h0128e01_0  
mkl                       2018.0.1             h19d6760_4  
ncurses                   6.0                  h9df7e31_2  
numpy                     1.13.3           py27hbcc08e0_0  
openssl                   1.0.2n               hb7f436b_0  
pcre                      8.41                 hc27e229_1  
pip                       9.0.1            py27ha730c48_4  
pycosat                   0.6.3            py27ha4109ae_0  
pycparser                 2.18             py27hefa08c5_1  
pyopenssl                 17.5.0           py27hcee3be0_0  
pyparsing                 2.2.0            py27hf1513f8_1  
pyqt                      5.6.0            py27h4b1e83c_5  
pysocks                   1.6.7            py27he2db6d2_1  
python                    2.7.14              h1571d57_29  
python-dateutil           2.6.1            py27h4ca5741_1  
pytz                      2017.3           py27h001bace_0  
qt                        5.6.2               h974d657_12  
readline                  7.0                  ha6073c6_4  
requests                  2.18.4           py27hc5b0589_1  
ruamel_yaml               0.11.14          py27h672d447_2  
setuptools                36.5.0           py27h68b189e_0  
singledispatch            3.4.0.3          py27h9bcb476_0  
sip                       4.18.1           py27he9ba0ab_2  
six                       1.11.0           py27h5f960f1_1  
sqlite                    3.20.1               hb898158_2  
ssl_match_hostname        3.5.0.1          py27h4ec10b9_2  
subprocess32              3.2.7            py27h373dbce_0  
tk                        8.6.7                hc745277_3  
tornado                   4.5.2            py27h97b179f_0  
urllib3                   1.22             py27ha55213b_0  
wheel                     0.30.0           py27h2bc6bb2_1  
xz                        5.2.3                h55aa19d_2  
yaml                      0.1.7                had09818_2  
zlib                      1.2.11               ha838bed_2  

Even though this problem is found in some online forums, none includes a solution. :frowning:

You will need to make sure import matplotlib.pyplot works in Python. You may have to install some dependencies yourself which it will tell you. That’s what I did before but I don’t remember the details.

In the above error, there is

...
) <type 'exceptions.ImportError'>
ImportError("/lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/../../.././libpng16.so.16)",)
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/pyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
    import matplotlib.font_manager as font_manager
  File "/home/vish/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
    from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
...

Is this the problem that my julia is looking at system path for ZLIB??

One more update: this method works - https://github.com/JuliaPy/PyPlot.jl/issues/151#issuecomment-287226101

Use this to start julia:

LD_PRELOAD=${HOME}/.julia/v0.6/Conda/deps/usr/lib/libz.so julia

But it’s annoying to do this every time.
And i don’t know why, but when I start REPL with this, after plotting, my REPL becomes sooooper slow :expressionless:

the slowness is a bug in the recent matplotlib Qt interface. If you can, try v2.0.x of matplotlib, or else use a different backend for PyPlot.

$ ~/.julia/v0.6/Conda/deps/usr/bin/python
Python 2.7.14 |Anaconda, Inc.| (default, Dec  7 2017, 17:05:42) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as p
>>> p.plot( [1,2,3] )
[<matplotlib.lines.Line2D object at 0x7fea78e38390>]
>>> p.show()
>>> 

This works fine.

So it’s definitely the problem with where julia tries to open LIBZ from, right?

Seems like it from the issue you linked https://github.com/JuliaPy/PyPlot.jl/issues/151. I have never had this error before but @stevengj says this is the problem and recommends some solutions in the same issue, and he is waaaay more knowledgeable than me!