我想在Julia代码中使用一系列Python库。我一直在尝试使用PyCall来访问我已经安装在我的(windows) PC上的Python库;然而,我一直无法重新配置PyCall,使其脱离Julia专用的默认Python发行版。按照文档(https://github.com/JuliaPy/PyCall.jl)中的步骤,我在Julia中尝试了以下操作:
using Pkg
ENV["PYTHON"] = raw"C:\Users\catod\AppData\Local\Programs\Python\Python37-32\python.exe"
Pkg.build(PyCall)在退出并重新进入Julia之后,我尝试运行
using PyCall
plt = pyimport("matplotlib.pyplot")但得到以下错误消息:
ERROR: LoadError: PyError (PyImport_ImportModule
The Python package matplotlib.pyplot could not be imported 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.
) <class 'ModuleNotFoundError'>
ModuleNotFoundError("No module named 'matplotlib'")有谁能指出我哪里错了吗?
发布于 2020-10-10 17:19:50
这里有两个问题:
pip安装matplotlib
matplolib,你的代码也会在你尝试show第一个绘图的地方崩溃,因为不加载PyPlot就不可能将matlplotlib直接嵌入到Julia中。因此,你的代码应该是:使用PyCall使用PyPlot plt = pyimport("matplotlib.pyplot")
现在,另一个问题是,通常情况下,PyPlot会通过conda安装它需要的所有包。由于您尝试使用普通的Python,因此需要手动安装这些包。
备注
基本上,人们应该使用Anaconda来获得良好的Julia-Python体验,因为在这种情况下,有许多工具可以自动化集成。
使用Julia内置的Python是最简单的选择。我绝对推荐:
ENV["PYTHON"] = ""
pkg"build PyCall"
using Conda
Conda.add("matplotlib")另一个相对简单的选择是Anaconda。安装您自己的Anaconda并将ENV["PYTHON"]设置为它,build它并享受它。使用非Anaconda Python永远不会很好,因为您仍然无法使用Conda.jl来管理安装。
https://stackoverflow.com/questions/64288845
复制相似问题