Question about PyPlot

Dear all,

When I use PyPlot, It always gets an error.

julia> using PyPlot

julia> x = 1:2:100
1:2:99

julia> y = 2x;

julia> plot(x, y)
ERROR: ArgumentError: hasproperty of NULL PyObject
Stacktrace:
 [1] pyhasproperty(o::PyCall.PyObject, s::String)
   @ PyCall ~/.julia/packages/PyCall/7a7w0/src/PyCall.jl:363
 [2] hasproperty
   @ ~/.julia/packages/PyCall/7a7w0/src/PyCall.jl:369 [inlined]
 [3] plot(::StepRange{Int64, Int64}, ::Vararg{Any}; kws::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ PyPlot ~/.julia/packages/PyPlot/XaELc/src/PyPlot.jl:174
 [4] plot(::StepRange{Int64, Int64}, ::Vararg{Any})
   @ PyPlot ~/.julia/packages/PyPlot/XaELc/src/PyPlot.jl:174
 [5] top-level scope
   @ REPL[33]:1


I run in Mac M1.

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

Try adding

ENV["PYTHON"]=""

to your startup.jl

Personally, I approach using python (on its own or through julia) slightly differently when I use mac or linux (or other unix). These OSs frequently come with some preinstalled python as part of the system. Using that pre-installed python has always been deeply problematic as changing/installing packages for it usually borks the rest of the system that tries to use it for some low-level admin purposes. To get around that, I always install a separate python and do all my non-sys-admin work (scientific or web dev) in that other python. You can do that with conda/anaconda (pretty common suggestion) or with virtenv (my preference). Anyway, all this wall of text to say “please avoid using your pre-installed python”. (As an aside, julia’s default installation instructions do not have this issue, because (1) julia is not preinstalled and (2) julia works well as a “local” executable independent of the rest of the system especially thanks to (3) its package management style)

Thus, my preferred solution to your problem would be to instruct Julia to not use the system python. You can do that by opening a fresh Julia session, ensuring the PYTHON global variable is unset by typing ENV["PYTHON"]="" and then reinstalling or rebuilding PyCall and PyPlot. All in all, it might look something like:

ENV["PYTHON"]=""
Pkg.build("PyCall")
Pkg.build("PyPlot") # or you can remove and reinstall PyCall/PyPlot

This will download a whole new python installation for you. Yes, this is ridiculously wasteful, but that type of “walling off” your OSs python from your “work” python is necessary to avoid different systems clashing with each other. It is something I do even if I am using just python, without using any Julia.

EDIT: Notice I am not really solving your exact problem, as I indeed have no idea why your matplotlib is inaccessible or not installed. I am just suggesting compartmentalizing python so that this type of problems is less probable to arise in the first place.

julia> ENV["PYTHON"]="/Library/Frameworks/Python.framework/Versions/3.10/bin/python3"
"/Library/Frameworks/Python.framework/Versions/3.10/bin/python3"

julia> Pkg.build("PyCall")
    Building Conda ─→ `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/6e47d11ea2776bc5627421d59cdcc1296c058071/build.log`
    Building PyCall → `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/1fc929f47d7c151c839c5fc1375929766fb8edcc/build.log`
Precompiling project...
  1 dependency successfully precompiled in 3 seconds (551 already precompiled, 2 skipped during auto due to previous errors)

julia> Pkg.build("PyPlot") 
    Building Conda ─→ `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/6e47d11ea2776bc5627421d59cdcc1296c058071/build.log`
    Building PyCall → `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/1fc929f47d7c151c839c5fc1375929766fb8edcc/build.log`
Precompiling project...
  1 dependency successfully precompiled in 3 seconds (551 already precompiled, 2 skipped during auto due to previous errors)

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

It seems not work for me and I have installed matplotlib.

✦ ➜ python
Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>>

This error means just what it says. If you run python3 and do import matplotlib in Python, does it work?

By default, on a Mac, PyCall and PyPlot normally install their own Python distro via conda-forge, and then will install Matplotlib for you. You must have overridden this by setting the PYTHON environment variable manually, which is really only for people who know what they are doing.

You don’t need to do it in your startup.jl, and by itself this will do nothing because it only affects building PyCall. You should instead do:

julia> ENV["PYTHON"]=""

pkg> build PyCall

and then re-start Julia.

That’s why PyCall defaults to using Conda.jl to install its own Python on Mac and Windows.

1 Like

Don’t do this — this is the Python that is not working. Set ENV["PYTHON"]="" so that Julia can install its own Python.

And you need to restart Julia after rebuilding PyCall.

3 Likes

Are you sure this is the same python executable? (Type which python to see which Python you are running.)

✦2 ➜ which python
python: aliased to /Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
(base)

As was already mentioned above, this is the opposite of what I suggested. Avoid using your system’s python install for non-sysadmin tasks, as it would inevitably lead to clashes, mismatched versions, and general unreliability. With enough perseverance I am sure you can make it work even with the pre-installed system’s python, but you probably would not get much help from the community as it is one of the ways we specifically try to avoid.

1 Like

Thanks for all your help. After I set ENV["PYTHON"] = "",now it works well.
Thanks very much. @ctkelley @Krastanov @stevengj

1 Like

@Krastanov @stevengj @ctkelley I have another problem when using RCall. Can you help me to solve this problem?

julia> using RCall
[ Info: Precompiling RCall [6f49c342-dc21-5d91-9882-a32aef131414]

WARNING: Method definition Type##kw(Any, Type{Base.MPFR.BigFloat}, Base.Irrational{:invπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition Type##kw(Any, Type{Base.MPFR.BigFloat}, Base.Irrational{:twoinvπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:invsqrt2}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:sqrt4Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:sqrt2Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:fourinvπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:loghalf}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:logtwo}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:log2Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:inv4Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:twoπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:sqrtπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:sqrt3}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:sqrthalfπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:invsqrt2Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:inv2Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:logπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:log4Ď€}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:halfπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:fourπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:quartπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:sqrt2}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:invπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float32})(Base.Irrational{:twoinvπ}) in module StatsFuns at irrationals.jl:192 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:invsqrt2}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:log2Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:sqrt4Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:sqrt2Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:fourinvπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:logtwo}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants at irrationals.jl:175.
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:inv4Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:twoπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:sqrtπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:sqrt3}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:sqrthalfπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:invsqrt2Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:inv2Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:logπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:log4Ď€}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:halfπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:fourπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:quartπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:sqrt2}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:loghalf}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:invπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Base.MPFR.BigFloat})(Base.Irrational{:twoinvπ}) in module StatsFuns at irrationals.jl:182 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:invsqrt2}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:sqrt4Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:sqrt2Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:fourinvπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:loghalf}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:logtwo}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:log2Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:inv4Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:twoπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:sqrtπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:sqrt3}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:sqrthalfπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:invsqrt2Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:inv2Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:logπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:log4Ď€}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:halfπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:fourπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:quartπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:sqrt2}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:invπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::Type{Float64})(Base.Irrational{:twoinvπ}) in module StatsFuns at irrationals.jl:191 overwritten in module IrrationalConstants on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

ERROR: LoadError: RCall not properly installed. Please run Pkg.build("RCall")
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] top-level scope
   @ ~/.julia/packages/RCall/6kphM/src/RCall.jl:36
 [3] include
   @ ./Base.jl:418 [inlined]
 [4] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing)
   @ Base ./loading.jl:1318
 [5] top-level scope
   @ none:1
 [6] eval
   @ ./boot.jl:373 [inlined]
 [7] eval(x::Expr)
   @ Base.MainInclude ./client.jl:453
 [8] top-level scope
   @ none:1
in expression starting at /Users/zhangdeliang/.julia/packages/RCall/6kphM/src/RCall.jl:2
ERROR: Failed to precompile RCall [6f49c342-dc21-5d91-9882-a32aef131414] to /Users/zhangdeliang/.julia/compiled/v1.7/RCall/jl_QExoBD.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] compilecache(pkg::Base.PkgId, path::String, internal_stderr::IO, internal_stdout::IO, ignore_loaded_modules::Bool)
   @ Base ./loading.jl:1466
 [3] compilecache(pkg::Base.PkgId, path::String)
   @ Base ./loading.jl:1410
 [4] _require(pkg::Base.PkgId)
   @ Base ./loading.jl:1120
 [5] require(uuidkey::Base.PkgId)
   @ Base ./loading.jl:1013
 [6] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:997

julia> using Pkg

julia> Pkg.build("RCall")
    Building Conda → `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/6e47d11ea2776bc5627421d59cdcc1296c058071/build.log`
    Building RCall → `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/72fddd643785ec1f36581cbc3d288529b96e99a7/build.log`
ERROR: Error building `RCall`:
ERROR: could not load library "/Library/Frameworks/R.framework/Resources/lib/libR.dylib"
dlopen(/Library/Frameworks/R.framework/Resources/lib/libR.dylib, 0x0001): tried: '/Library/Frameworks/R.framework/Resources/lib/libR.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libR.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
ERROR: LoadError: Try adding /Library/Frameworks/R.framework/Resources/lib to the "LD_LIBRARY_PATH" environmental variable and restarting Julia.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] validate_libR(libR::String)
   @ Main ~/.julia/packages/RCall/6kphM/deps/setup.jl:26
 [3] locate_libR(Rhome::SubString{String})
   @ Main ~/.julia/packages/RCall/6kphM/deps/setup.jl:43
 [4] top-level scope
   @ ~/.julia/packages/RCall/6kphM/deps/build.jl:58
 [5] include(fname::String)
   @ Base.MainInclude ./client.jl:451
 [6] top-level scope
   @ none:5
in expression starting at /Users/zhangdeliang/.julia/packages/RCall/6kphM/deps/build.jl:11

caused by: could not load library "/Library/Frameworks/R.framework/Resources/lib/libR.dylib"
dlopen(/Library/Frameworks/R.framework/Resources/lib/libR.dylib, 0x0001): tried: '/Library/Frameworks/R.framework/Resources/lib/libR.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libR.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
Stacktrace:
 [1] dlopen(s::String, flags::UInt32; throw_error::Bool)
   @ Base.Libc.Libdl ./libdl.jl:117
 [2] dlopen (repeats 2 times)
   @ ./libdl.jl:117 [inlined]
 [3] validate_libR(libR::String)
   @ Main ~/.julia/packages/RCall/6kphM/deps/setup.jl:16
 [4] locate_libR(Rhome::SubString{String})
   @ Main ~/.julia/packages/RCall/6kphM/deps/setup.jl:43
 [5] top-level scope
   @ ~/.julia/packages/RCall/6kphM/deps/build.jl:58
 [6] include(fname::String)
   @ Base.MainInclude ./client.jl:451
 [7] top-level scope
   @ none:5
Stacktrace:
  [1] pkgerror(msg::String)
    @ Pkg.Types /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Types.jl:68
  [2] (::Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec})()
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:962
  [3] withenv(::Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, ::Pair{String, String}, ::Vararg{Pair{String}})
    @ Base ./env.jl:172
  [4] (::Pkg.Operations.var"#99#103"{String, Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.PackageSpec})()
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1506
  [5] with_temp_env(fn::Pkg.Operations.var"#99#103"{String, Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.PackageSpec}, temp_env::String)
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1390
  [6] (::Pkg.Operations.var"#98#102"{Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.Context, Pkg.Types.PackageSpec, String, Pkg.Types.Project, String})(tmp::String)
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1469
  [7] mktempdir(fn::Pkg.Operations.var"#98#102"{Bool, Bool, Bool, Pkg.Operations.var"#62#67"{Bool, Pkg.Types.Context, String, Pkg.Types.PackageSpec}, Pkg.Types.Context, Pkg.Types.PackageSpec, String, Pkg.Types.Project, String}, parent::String; prefix::String)
    @ Base.Filesystem ./file.jl:750
  [8] mktempdir(fn::Function, parent::String) (repeats 2 times)
    @ Base.Filesystem ./file.jl:748
  [9] sandbox(fn::Function, ctx::Pkg.Types.Context, target::Pkg.Types.PackageSpec, target_path::String, sandbox_path::String, sandbox_project_override::Pkg.Types.Project; force_latest_compatible_version::Bool, allow_earlier_backwards_compatible_versions::Bool, allow_reresolve::Bool)
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1435
 [10] sandbox(fn::Function, ctx::Pkg.Types.Context, target::Pkg.Types.PackageSpec, target_path::String, sandbox_path::String, sandbox_project_override::Pkg.Types.Project)
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:1432
 [11] build_versions(ctx::Pkg.Types.Context, uuids::Set{Base.UUID}; verbose::Bool)
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:943
 [12] build(ctx::Pkg.Types.Context, uuids::Set{Base.UUID}, verbose::Bool)
    @ Pkg.Operations /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/Operations.jl:822
 [13] build(ctx::Pkg.Types.Context, pkgs::Vector{Pkg.Types.PackageSpec}; verbose::Bool, kwargs::Base.Pairs{Symbol, Base.TTY, Tuple{Symbol}, NamedTuple{(:io,), Tuple{Base.TTY}}})
    @ Pkg.API /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:992
 [14] build(pkgs::Vector{Pkg.Types.PackageSpec}; io::Base.TTY, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Pkg.API /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:149
 [15] build(pkgs::Vector{Pkg.Types.PackageSpec})
    @ Pkg.API /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:144
 [16] #build#99
    @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:142 [inlined]
 [17] build
    @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:142 [inlined]
 [18] #build#98
    @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:141 [inlined]
 [19] build(pkg::String)
    @ Pkg.API /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Pkg/src/API.jl:141
 [20] top-level scope
    @ REPL[8]:1

I have solved the problem. Thanks.

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:`R RHOME`/lib"