Error installing PyPlot: The Python package mpl_toolkits.mplot3d could not be found by pyimport

pyplot

#1

I’m trying to install PyPlot on my system ( OSX 10.10 ) and is getting this error:

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin13.4.0

julia> Pkg.update()
INFO: Updating METADATA...
INFO: Computing changes...
INFO: No packages to install, update or remove

julia> Pkg.add( "PyPlot" )
INFO: Installing ColorTypes v0.6.4
INFO: Installing Colors v0.8.1
INFO: Installing FixedPointNumbers v0.4.2
INFO: Installing LaTeXStrings v0.2.1
INFO: Installing PyPlot v2.3.2
INFO: Installing Reexport v0.0.3
INFO: Building Conda
INFO: Building PyCall
INFO: PyCall is using /Users/vish/.julia/v0.6/Conda/deps/usr/bin/python (Python 2.7.13) at /Users/vish/.julia/v0.6/Conda/deps/usr/bin/python, libpython = /Users/vish/.julia/v0.6/Conda/deps/usr/lib/libpython2.7
INFO: /Users/vish/.julia/v0.6/PyCall/deps/deps.jl has not changed
INFO: /Users/vish/.julia/v0.6/PyCall/deps/PYTHON has not changed
INFO: Package database updated

julia> using PyPlot
INFO: Precompiling module PyPlot.
INFO: Installing matplotlib via the Conda matplotlib package...
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment /Users/vish/.julia/v0.6/Conda/deps/usr:

The following NEW packages will be INSTALLED:

    matplotlib: 2.1.0-py27h3078195_0

ERROR: InitError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

The Python package mpl_toolkits.mplot3d could not be found by pyimport. Usually this means
that you did not install mpl_toolkits.mplot3d 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 mpl_toolkits.mplot3d module, you can
use `pyimport_conda("mpl_toolkits.mplot3d", PKG)`, where PKG is the Anaconda
package the contains the module mpl_toolkits.mplot3d, 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('No module named mpl_toolkits.mplot3d',)

Stacktrace:
 [1] pyerr_check at /Users/vish/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]
 [2] pyerr_check at /Users/vish/.julia/v0.6/PyCall/src/exception.jl:61 [inlined]
 [3] macro expansion at /Users/vish/.julia/v0.6/PyCall/src/exception.jl:81 [inlined]
 [4] pyimport(::String) at /Users/vish/.julia/v0.6/PyCall/src/PyCall.jl:374
 [5] __init__() at /Users/vish/.julia/v0.6/PyPlot/src/init.jl:205
 [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:491
 [9] require(::Symbol) at ./loading.jl:398
during initialization of module PyPlot

I have

julia> PyCall.python
"/Users/vish/.julia/v0.6/Conda/deps/usr/bin/python"

julia> versioninfo()
Julia Version 0.6.0
Commit 903644385b (2017-06-19 13:05 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i5-4570R CPU @ 2.70GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

julia> Conda.list()
# packages in environment at /Users/vish/.julia/v0.6/Conda/deps/usr:
#
asn1crypto                0.22.0                   py27_0  
backports                 1.0              py27hb4f9756_1  
backports.functools_lru_cache 1.4              py27h2aca819_1  
backports_abc             0.5              py27h6972548_0  
certifi                   2017.7.27.1      py27h482ffc0_0  
cffi                      1.10.0                   py27_0  
conda                     4.3.27           py27h94ab009_0  
conda-env                 2.6.0                h36134e3_0  
cryptography              1.8.1                    py27_0  
cycler                    0.10.0           py27hfc73c78_0  
enum34                    1.1.6                    py27_0  
freetype                  2.8                  h143eb01_0  
functools32               3.2.3.2          py27h8ceab06_1  
idna                      2.5                      py27_0  
intel-openmp              2018.0.0             hdd0ccc9_7  
ipaddress                 1.0.18                   py27_0  
libcxx                    4.0.1                h579ed51_0  
libcxxabi                 4.0.1                hebd6815_0  
libgfortran               3.0.1                h93005f0_2  
libpng                    1.6.32               hce72d48_2  
libsodium                 1.0.13               hba5e272_2  
matplotlib                2.1.0            py27h3078195_0  
mkl                       2018.0.0             hc285769_4  
numpy                     1.13.3           py27h62f9060_0  
openssl                   1.0.2l                        0  
packaging                 16.8                     py27_0  
pip                       9.0.1                    py27_1  
pycosat                   0.6.2                    py27_0  
pycparser                 2.17                     py27_0  
pyopenssl                 17.0.0                   py27_0  
pyparsing                 2.1.4                    py27_0  
python                    2.7.13                        0  
python-dateutil           2.6.1            py27hd56c96b_1  
pytz                      2017.2           py27hb891d23_1  
readline                  6.2                           2  
requests                  2.14.2                   py27_0  
ruamel_yaml               0.11.14                  py27_1  
setuptools                27.2.0                   py27_0  
singledispatch            3.4.0.3          py27he22c18d_0  
six                       1.10.0                   py27_0  
sqlite                    3.13.0                        0  
ssl_match_hostname        3.5.0.1          py27h8780752_2  
subprocess32              3.2.7            py27h24b2887_0  
tk                        8.5.18                        0  
tornado                   4.5.2            py27h29aec9e_0  
wheel                     0.29.0                   py27_0  
yaml                      0.1.6                         0  
zeromq                    4.2.2                h131e0f7_1  
zlib                      1.2.11               h60db283_

Using PyPlot => ERROR: InitError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)
#2

After a bit of googling, I found this issue related to Conda
https://github.com/ContinuumIO/anaconda-issues/issues/6423

And the current workaround is to execute

julia> Libdl.dlopen( "/Users/vish/.julia/v0.6/Conda/deps/usr/lib/libiomp5.dylib" )
julia> Libdl.dlopen( "/Users/vish/.julia/v0.6/Conda/deps/usr/lib/libmkl_intel_thread.dylib" )

before using plot.

But as a side effect, new problem came while installing other packages

julia> Pkg.add( "Distributions" )
 INFO: Cloning cache of Calculus from https://github.com/johnmyleswhite/Calculus.jl.git
INFO: Cloning cache of DataStructures from https://github.com/JuliaCollections/DataStructures.jl.git
INFO: Cloning cache of Distributions from https://github.com/JuliaStats/Distributions.jl.git
INFO: Cloning cache of PDMats from https://github.com/JuliaStats/PDMats.jl.git
INFO: Cloning cache of QuadGK from https://github.com/JuliaMath/QuadGK.jl.git
INFO: Cloning cache of Rmath from https://github.com/JuliaStats/Rmath.jl.git
INFO: Cloning cache of SpecialFunctions from https://github.com/JuliaMath/SpecialFunctions.jl.git
INFO: Cloning cache of StatsBase from https://github.com/JuliaStats/StatsBase.jl.git
INFO: Cloning cache of StatsFuns from https://github.com/JuliaStats/StatsFuns.jl.git
INFO: Installing Calculus v0.2.2
INFO: Installing DataStructures v0.7.2
INFO: Installing Distributions v0.14.2
INFO: Installing PDMats v0.7.0
INFO: Installing QuadGK v0.1.3
INFO: Installing Rmath v0.3.0
INFO: Installing SpecialFunctions v0.3.4
INFO: Installing StatsBase v0.19.0
INFO: Installing StatsFuns v0.5.0
INFO: Building SpecialFunctions
dyld: lazy symbol binding failed: Symbol not found: _mkl_serv_lock
  Referenced from: /Users/vish/.julia/v0.6/Conda/deps/usr/lib/libmkl_intel_thread.dylib
  Expected in: flat namespace

dyld: Symbol not found: _mkl_serv_lock
  Referenced from: /Users/vish/.julia/v0.6/Conda/deps/usr/lib/libmkl_intel_thread.dylib
  Expected in: flat namespace
^[[C

#3

So I put this code into my juliarc.jl and then load/unload the dylibs on demand:

# For Conda problem
function load_conda_dylib()
    dylib_list = [
        "/Users/vish/.julia/v0.6/Conda/deps/usr/lib/libiomp5.dylib",
        "/Users/vish/.julia/v0.6/Conda/deps/usr/lib/libmkl_intel_thread.dylib"
    ]

    return Libdl.dlopen.( dylib_list )
    # Libdl.dlopen( "/Users/vish/.julia/v0.6/Conda/deps/usr/lib/libiomp5.dylib" )
    # Libdl.dlopen( "/Users/vish/.julia/v0.6/Conda/deps/usr/lib/libmkl_intel_thread.dylib" )

    # info( "Loaded ", length(conda_dylibs), " dylibs" )
end

function unload_conda_dylibs( conda_dylibs )
    if ~isempty(conda_dylibs)
        Libdl.dlclose.( conda_dylibs )
        info( "Unloaded ", length(conda_dylibs), " dylibs" )
        empty!( conda_dylibs )
    else
        info( "No dylib to unload" )
    end
end

#4

If it helps, there is one more convenient fix, that seems not to be breaking anything:

run(`install_name_tool -change @rpath/libiomp5.dylib @loader_path/libiomp5.dylib $(Pkg.dir("Conda", "deps/usr/lib/libmkl_intel_thread.dylib"))`)

#5

I have a similar problem with installing PyPlot for Julia 6.0 on Windows.

Pkg.add("PyPlot") is OK, but using PyPlot gives the error:

ERROR: InitError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

The Python package mpl_toolkits.mplot3d could not be found by pyimport. Usually this means
that you did not install mpl_toolkits.mplot3d 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 mpl_toolkits.mplot3d module, you can
use `pyimport_conda("mpl_toolkits.mplot3d", PKG)`, where PKG is the Anaconda
package the contains the module mpl_toolkits.mplot3d, 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('No module named mpl_toolkits.mplot3d',)

Please, could you suggest a solution for those
who use Windows and rely on the Julia-specific Python distribution?


#6

Run the code from my earlier post right after you get this error and re-launch Julia.
After that run update Conda.update() and Pkg.build("PyCall") just to make sure all Conda packages are installed and linked


#7

I did it but got the error:

julia> run(`install_name_tool -change @rpath/libiomp5.dylib @loader_path/libiomp5.dylib $(Pkg.dir("Conda", "deps/usr/lib/libmkl_intel_thread
.dylib"))`)
ERROR: could not spawn `install_name_tool -change @rpath/libiomp5.dylib @loader_path/libiomp5.dylib 'C:\Users\TURTLE\.julia\v0.6\Conda\deps\us
r\lib\libmkl_intel_thread.dylib'`: no such file or directory (ENOENT)
Stacktrace:
 [1] _jl_spawn(::String, ::Array{String,1}, ::Ptr{Void}, ::Base.Process, ::RawFD, ::RawFD, ::RawFD) at .\process.jl:360
 [2] #375 at .\process.jl:512 [inlined]
 [3] setup_stdio(::Base.##375#376{Cmd}, ::Tuple{RawFD,RawFD,RawFD}) at .\process.jl:499
 [4] #spawn#374(::Nullable{Base.ProcessChain}, ::Function, ::Cmd, ::Tuple{RawFD,RawFD,RawFD}) at .\process.jl:511
 [5] run(::Cmd) at .\process.jl:650

#8

I assume the first problem is, that there is no such tool for Windows.


#9

indeed I’ve overlooked the fact you have installation on Windows
very quick fix would be not to use Conda.jl but external Python installation


#10

See https://github.com/JuliaPy/PyPlot.jl/issues/151


#11

It is disappointing experience that a plain Windows user
with a fresh install of Julia 6.0 cannot seamlessly install PyPlot.

For the record, I also went back both to Julia 5.2 and Julia 4.7
(where it used to work at some point), and now could not install it either.


#12

After updating my packages, I sometimes get this error:

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> using 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/febbo/.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/febbo/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/../../.././libpng16.so.16)",)
  File "/home/febbo/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/matplotlib/pyplot.py", line 29, in <module>
    import matplotlib.colorbar
  File "/home/febbo/.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/febbo/.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/febbo/.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/febbo/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]
 [2] pyerr_check at /home/febbo/.julia/v0.6/PyCall/src/exception.jl:61 [inlined]
 [3] macro expansion at /home/febbo/.julia/v0.6/PyCall/src/exception.jl:81 [inlined]
 [4] pyimport(::String) at /home/febbo/.julia/v0.6/PyCall/src/PyCall.jl:374
 [5] __init__() at /home/febbo/.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_search_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at ./loading.jl:236
 [9] _require(::Symbol) at ./loading.jl:434
 [10] require(::Symbol) at ./loading.jl:398
during initialization of module PyPlot

Since I have the new matlibplot installed on a python installation, I do this

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> ENV["PYTHON"]="/usr/bin/python"
"/usr/bin/python"

julia> Pkg.build("PyCall")
INFO: Building Conda
INFO: Building PyCall
INFO: PyCall is using /usr/bin/python (Python 2.7.12) at /usr/bin/python, libpython = libpython2.7
INFO: /home/febbo/.julia/v0.6/PyCall/deps/deps.jl has been updated
INFO: /home/febbo/.julia/v0.6/PyCall/deps/PYTHON has been updated

julia> using PyPlot
INFO: Recompiling stale cache file /home/febbo/.julia/lib/v0.6/PyCall.ji for module PyCall.
INFO: Recompiling stale cache file /home/febbo/.julia/lib/v0.6/PyPlot.ji for module PyPlot.

julia>

#13

It looks like Anaconda no longer installs the mpl_toolkits module (which installs mplot3d) by default when you install matplotlib, at least on on some systems.

I just ran into this problem on Windows: I had to do using Conda; Conda.add("basemap") to install mpl_toolkits, which forced a downgrade of the numpy package.

Probably PyPlot should be updated to work (with 3d plotting disabled) if mplot3d is not available.


PyPlot Continuing?
Call Python functions from Julia / Lorenz Attractor Example