Reputation: 85
I'm wanting to use a range of Python libraries in my Julia code. I've been trying to use PyCall to access the Python libraries I have installed on my (windows) PC already; however, I have been unable to reconfigure PyCall away from its default Python distribution which is private to Julia. Following the steps in the documentation (https://github.com/JuliaPy/PyCall.jl), I attempted the following in Julia:
using Pkg
ENV["PYTHON"] = raw"C:\Users\catod\AppData\Local\Programs\Python\Python37-32\python.exe"
Pkg.build(PyCall)
After exiting and reentering Julia, I attempted to run
using PyCall
plt = pyimport("matplotlib.pyplot")
but got the following error message:
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'")
Can anyone point out where I have gone wrong?
Upvotes: 5
Views: 2579
Reputation: 42214
There are two problems here:
Matplotlib is not installed in your Python installation. You need to run (however I definitely recommend Anaconda instead - see notes at the end)
pip install matplotlib
Even if you manage to install matplolib
your code will very ugly crash where you try to show
the first plot because it is not possible to embed matlplotlib
directly to Julia without loading PyPlot
. Hence your code should be:
using PyCall
using PyPlot
plt = pyimport("matplotlib.pyplot")
now another problem is that normally PyPlot
is installing all packages it needs via conda. Since you try to use a plain Python you will need to manually install those packages.
Basically, one should need to use Anaconda to have a good Julia-Python experience because in this scenario there are many tools that automate the integration.
Using the Julia-inbuilt Python is the easiest option. I definitely recommend:
ENV["PYTHON"] = ""
pkg"build PyCall"
using Conda
Conda.add("matplotlib")
Another relatively easy option is Anaconda. Install your own Anaconda and set ENV["PYTHON"]
to it, build
it and enjoy.
Using a non-Anaconda Python will never be nice because you will still be unable to use Conda.jl
to manage installation.
Upvotes: 2