JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
475 stars 87 forks source link

Error in Mac M1 #551

Closed zdlspace0528 closed 2 years ago

zdlspace0528 commented 2 years ago
julia> using PyPlot
ERROR: InitError: PyError (PyImport_ImportModule

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

PyCall is currently configured to use the Python version at:

/Library/Frameworks/Python.framework/Versions/3.10/bin/python3

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the matplotlib module.

One alternative is to re-configure PyCall to use a different Python
version on your system: set ENV["PYTHON"] to the path/name of the python
executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.

Another alternative is to configure PyCall to use a Julia-specific Python
distribution via the Conda.jl package (which installs a private Anaconda
Python distribution), which has the advantage that packages can be installed
and kept up-to-date via Julia.  As explained in the PyCall documentation,
set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then,
To install the matplotlib module, you can use `pyimport_conda("matplotlib", PKG)`,
where PKG is the Anaconda package that contains the module matplotlib,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).

) <class 'ImportError'>
ImportError('\n\nIMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!\n\nImporting the numpy C-extensions failed. This error can happen for\nmany reasons, often due to issues with your setup or how NumPy was\ninstalled.\n\nWe have compiled some common reasons and troubleshooting tips at:\n\n    https://numpy.org/devdocs/user/troubleshooting-importerror.html\n\nPlease note and check the following:\n\n  * The Python version is: Python3.10 from "/Library/Frameworks/Python.framework/Versions/3.10/bin/python3"\n  * The NumPy version is: "1.22.3"\n\nand make sure that they are the versions you expect.\nPlease carefully study the documentation linked above for further help.\n\nOriginal error was: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so, 0x0002): tried: \'/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so\' (mach-o file, but is an incompatible architecture (have \'arm64\', need \'x86_64\'))\n')
  File "/Users/zhangdeliang/matplotlib/lib/matplotlib/__init__.py", line 108, in <module>
    import numpy
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py", line 144, in <module>
    from . import core
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/__init__.py", line 49, in <module>
    raise ImportError(msg)

Stacktrace:
 [1] pyimport(name::String)
   @ PyCall ~/.julia/packages/PyCall/7a7w0/src/PyCall.jl:550
 [2] pyimport_conda(modulename::String, condapkg::String, channel::String)
   @ PyCall ~/.julia/packages/PyCall/7a7w0/src/PyCall.jl:708
 [3] pyimport_conda
   @ ~/.julia/packages/PyCall/7a7w0/src/PyCall.jl:707 [inlined]
 [4] __init__()
   @ PyPlot ~/.julia/packages/PyPlot/XaELc/src/init.jl:185
 [5] _include_from_serialized(path::String, depmods::Vector{Any})
   @ Base ./loading.jl:768
 [6] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String)
   @ Base ./loading.jl:854
 [7] _require(pkg::Base.PkgId)
   @ Base ./loading.jl:1097
 [8] require(uuidkey::Base.PkgId)
   @ Base ./loading.jl:1013
 [9] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:997
during initialization of module PyPlot
zdlspace0528 commented 2 years ago
julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.5.0)
  CPU: Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, westmere)

(@v1.7) pkg> status PyPlot
      Status `~/.julia/environments/v1.7/Project.toml`
  [d330b81b] PyPlot v2.10.0

(@v1.7) pkg> status PyCall
      Status `~/.julia/environments/v1.7/Project.toml`
  [438e738f] PyCall v1.93.1

(@v1.7) pkg> status Conda
      Status `~/.julia/environments/v1.7/Project.toml`
  [8f4d0f93] Conda v1.7.0
stevengj commented 2 years ago

See

https://discourse.julialang.org/t/question-about-pyplot/81738/8?u=stevengj

You need to use a Python that has Matplotlib installed. Most simply, let Julia install its own Python (which happens by default, so you must have explicitly overridden this behavior by setting the PYTHON environment variable).