I’m having a bit of a bizarre error. I am trying to write a script in which I use the PyPlot package. if I type using PyPlot
into the REPL, it runs without a problem as far as I can tell.
However, when I have the exact same command in a script, I instead get an error:
InitError: PyError (PyImport_ImportModule
The Python package matplotlib could not be found 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:
/usr/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 the contains the module matplotlib,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).
) <class 'ModuleNotFoundError'>
ModuleNotFoundError("No module named 'matplotlib'",)
during initialization of module PyPlot
pyimport(::String) at PyCall.jl:536
pyimport_conda(::String, ::String, ::String) at PyCall.jl:694
pyimport_conda at PyCall.jl:693 [inlined]
__init__() at init.jl:179
_include_from_serialized(::String, ::Array{Any,1}) at loading.jl:692
_require_search_from_serialized(::Base.PkgId, ::String) at loading.jl:776
_require(::Base.PkgId) at loading.jl:1001
require(::Base.PkgId) at loading.jl:922
require(::Module, ::Symbol) at loading.jl:917
I definitely have matplotlib installed as I have checked this from my Linux terminal. Now I have typed in ENV["PYTHON"]="/usr/bin/python"
into the REPL but this does not seem to solve the problem either.
That being said, typing using PyPlot
into the REPL runs without issue.