朱莉娅情节:PyCall.PyError(PyImport_ImportModule\n\nThe Python 包 matplotlib.pyplot 无法通过 pyimport 找到

Julia Plots: PyCall.PyError("PyImport_ImportModule\n\nThe Python package matplotlib.pyplot could not be found by pyimport

我正在尝试使用 pyplot() 后端在 Julia 中通过 plots() 进行绘图。这是我得到的错误。我对我需要做什么来解决这个问题感到困惑。谁能帮忙?

*LoadError: InitError: PyCall.PyError("PyImport_ImportModule\n\nThe Python package matplotlib.pyplot could not be found by pyimport. Usually this means\nthat you did not install matplotlib.pyplot in the Python version being used by PyCall.\n\nPyCall is currently configured to use the Julia-specific Python distribution\ninstalled by the Conda.jl package. To install the matplotlib.pyplot module, you can\nuse pyimport_conda(\"matplotlib.pyplot\", PKG), where PKG is the Anaconda\npackage the contains the module matplotlib.pyplot, or alternatively you can use the\nConda package directly (via using Conda followed by Conda.add etcetera).\n\nAlternatively, if you want to use a different Python distribution on your\nsystem, such as a system-wide Python (as opposed to the Julia-specific Python),\nyou can re-configure PyCall with that Python. As explained in the PyCall\ndocumentation, set ENV[\"PYTHON\"] 到 path/name 的 python executable\nyou 想使用 运行 Pkg.build(\"PyCall\"),然后重新启动 Julia。\n\n", PyCall.PyObject(Ptr{PyCall.PyObject_struct} @ 0x0000000166bd5f40), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00000001a99e3748), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00000001a99f75c8)) 在模块 PyPlot 初始化期间 *

这绝对是 "many Pythons" 问题。基本上在 Julia 中你总是有两个选择:

  1. 使用 Julia 内置的 Anaconda Python
  2. 使用外部 (Anaconda) Python 安装

我更喜欢第二种选择,因为大多数数据 science/scientific 计算机都有一个 Anaconda,而且我更喜欢有一个 Anaconda 来管理而不是许多 Anaconda。

广告 1。 当没有设置 PYTHON 环境变量时使用内置的 Python (在 Julia 控制台中查找 ENV["PYTHON"] 的值) 基本上通常首先要尝试的是按 ] 包管理器和 运行:

(v1.0) pkg> build PyCall

(v1.0) pkg> build PyPlot

广告 2。 设置 PYTHON 环境变量(我使用示例,典型路径)​​

Windows(或者去电脑管理里设置系统变量):

$ set PYTHON=C:\ProgramData\Anaconda3\python.exe

Linux(示例 Ubuntu 配置):

$ export PYTHON=/home/ubuntu/anaconda3/bin/python

Julia 控制台(在 Linux Ubuntu 上)

julia> ENV["PYTHON"]="/home/ubuntu/anaconda3/bin/python"

完成后转到 Julia 包管理器并重建包:

(v1.0) pkg> build PyCall

(v1.0) pkg> build PyPlot

应该可以。